SlideShare uma empresa Scribd logo
1 de 28
Baixar para ler offline
SQL Basics
Introduction to
Standard Query Language
SQL – What Is It?
Structured Query Language
Common Language For Variety of
Databases
ANSI Standard BUT….
Two Types of SQL
DML – Data Manipulation Language (SELECT)
DDL – Data Definition Language (CREATE
TABLE)
Where To Use
SQL*Plus
TOAD
SQL Navigator
ODBC Supported Connections
Excel
Access
Lotus 1-2-3

Heart of PL/SQL
Pros & Cons of SQL
Pros:
Very flexible
Universal (Oracle, Access, Paradox, etc)
Relatively Few Commands to Learn

Cons:
Requires Detailed Knowledge of the Structure
of the Database
Can Provide Misleading Results
Basic SQL Components
SELECT schema.table.column
FROM table alias

WHERE [conditions]
ORDER BY [columns]
;
Defines the end of an SQL statement
Some programs require it, some do not (TOAD Does
Not)
Needed only if multiple SQL statements run in a script

Optional Elements
SELECT Statement
SELECT Statement Defines WHAT is to be
returned (separated by commas)

Database Columns (From Tables or Views)
Constant Text Values
Formulas
Pre-defined Functions
Group Functions (COUNT, SUM, MAX, MIN, AVG)

“*” Mean All Columns From All Tables In the
FROM Statement
Example: SELECT state_code, state_name
FROM Statement
Defines the Table(s) or View(s) Used by
the SELECT or WHERE Statements
You MUST Have a FROM statement
Multiple Tables/Views are separated by
Commas
Examples
SELECT state_name, state_abbr
FROM states
SELECT *
FROM agencies
SELECT arithmetic_mean – minimum_value
FROM annual_summaries
WHERE Clause
Optional
Defines what records are to be included in the query
Uses Conditional Operators
=, >, >=, <, <=, != (<>)
BETWEEN x AND y
IN (list)
LIKE ‘%string’ (“%” is a wild-card)
IS NULL
NOT {BETWEEN / IN / LIKE / NULL}

Multiple Conditions Linked with AND & OR Statements
Strings Contained Within SINGLE QUOTES!!
AND & OR
Multiple WHERE conditions are Linked by AND /
OR Statements
“AND” Means All Conditions are TRUE for the
Record
“OR” Means at least 1 of the Conditions is TRUE
You May Group Statements with ( )
BE CAREFUL MIXING “AND” & “OR” Conditions
Examples with WHERE
SELECT *
FROM annual_summaries
WHERE sd_duration_code = ‘1’
SELECT state_name
FROM states
WHERE state_population > 15000000
More Examples
SELECT state_name, state_population
FROM states
WHERE state_name LIKE ‘%NORTH%’
SELECT *
FROM annual_summaries
WHERE sd_duration_code IN (‘1’, ‘W’, ‘X’)
AND annual_summary_year = 2000
Be Careful!
SELECT mo_mo_id, sd_duration_code
FROM annual_summaries
WHERE annual_summary_year = 2003
AND values_gt_pri_std > 0
OR values_gt_sec_std > 0
SELECT mo_mo_id, sd_duration_code
FROM annual_summaries
WHERE annual_summary_year = 2003
AND (values_gt_pri_std > 0
OR values_gt_sec_std > 0)
ORDER BY Statement
Defines How the Records are to be Sorted
Must be in the SELECT statement to be
ORDER BY
Default is to order in ASC (Ascending)
order
Can Sort in Reverse (Descending) Order
with “DESC” After the Column Name
ORDER BY Example
SELECT *
FROM agencies
ORDER BY agency_desc
SELECT cc_cn_stt_state_code, site_id
FROM sites
WHERE lut_land_use_type = ‘MOBILE’
ORDER BY cc_cn_stt_state_code DESC
Group Functions
Performs Common Mathematical
Operations on a Group of Records
Must define what Constitutes a Group by
Using the GROUP BY Clause
All non-Group elements in the SELECT
Statement Must be in the GROUP BY
Clause (Additional Columns are Optional)
Group By Example
SELECT si_si_id, COUNT(mo_id)
FROM monitors
GROUP BY si_si_id
SELECT AVG(max_sample_value)
FROM summary_maximums
WHERE max_level <= 3
AND max_ind = ‘REG’
GROUP BY ans_ans_id
OK, I understand How to Get Data
From 1 Table… What about
Multiple Tables?
V_MONITOR_ID

MONITORS

PARAMETERS

PARAMETER_CODE
PARAMETER_DESC

MO_ID
SI_SI_ID
PA_PARAMETER_CODE
POC

MO_ID
AIRS_MONITOR_ID
STATE_CODE
COUNTY_CODE
SITE_ID
PARAMETER_CODE
POC
Primary & Foreign Keys
Primary Keys
1 or More Columns Used to Uniquely Identify
a record.
All Columns Defined as PK’s MUST be
populated

Foreign Keys
Value on a table that references a Primary
Key from a different table
Primary & Foreign Keys
SITES

SI_ID%
SITE_LATITUDE
SITE_LONGITUDE
STREET_ADDRESS

PARAMETERS

PARAMETER_CODE%
PARAMETER_DESC

V_MONITOR_ID

MONITORS

MO_ID%
SI_SI_ID*
PA_PARAMETER_CODE*
POC

* = Foreign Key
% = Primary Key

MO_ID
STATE_CODE
COUNTY_CODE
SITE_ID
PARAMETER_CODE
POC
Joining Tables
MONITORS

PARAMETERS
MO_ID

SI_SI_ID

PA_PARAMETER_CODE

POC

Parameter_Code

Parameter_Desc

1

1

44201

1

44201

Ozone

2

1

42101

1

3

1

42101

2

4

2

81102

1

42101

CO

42401

SO2

5

2

44201

1

81102

PM10

6

3

42401

1

Default behavior is to show every possible combination between the two tables
Cartesian Join / Simple Join
SELECT mo_id, poc, parameter_desc
FROM monitors, parameters
MONITORS

PARAMETERS
MO_ID

SI_SI_ID

PA_PARAMETER_CODE

POC

Parameter_Code

Parameter_Desc

1

1

44201

1

44201

Ozone

2

1

42101

1

3

1

42101

2

4

2

81102

1

42101

CO

42401

SO2

5

2

44201

1

81102

PM10

6

3

42401

1
Joining Tables
SELECT mo_id, poc, parameter_desc
FROM monitors, parameters
WHERE pa_parameter_code = parameter_code
MONITORS

PARAMETERS
MO_ID

SI_SI_ID

PA_PARAMETER_CODE

POC

Parameter_Code

Parameter_Desc

1

1

44201

1

44201

Ozone

2

1

42101

1

3

1

42101

2

4

2

81102

1

42101

CO

42401

SO2

5

2

44201

1

81102

PM10

6

3

42401

1
Joining Tables
Joins Between Tables are Usually Based
on Primary / Foreign Keys
Make Sure Joins Between All Tables in the
FROM Clause Exist
List Joins Between Tables Before Other
Selection Elements
Aliases
“Shorthand” for Table or Column
References
SELECT Aliases Appear as Column
Headers in the Output
Aliases Cannot be Keywords
Previous SQL With Aliases
SELECT mo.mo_id, mo.poc, pa.parameter_desc parameter
FROM monitors mo, parameters pa
WHERE mo.pa_parameter_code = pa.parameter_code
Why Use an Alias?
Saves Typing
Good Internal Documentation
Better Headers
If the same column name exists on
multiple tables, SQL needs a way to know
which element you are referencing
(MO_MO_ID for example)
Recap
Basic Structural Elements
SELECT
FROM
WHERE
ORDER BY
GROUP BY

Selecting From Multiple Tables
Join Multiple Tables via Primary & Foreign Keys
Aliases

Mais conteúdo relacionado

Mais procurados

06.01 sql select distinct
06.01 sql select distinct06.01 sql select distinct
06.01 sql select distinctBishal Ghimire
 
Structure query language (sql)
Structure query language (sql)Structure query language (sql)
Structure query language (sql)Nalina Kumari
 
Complete Sql Server querries
Complete Sql Server querriesComplete Sql Server querries
Complete Sql Server querriesIbrahim Jutt
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQLMahir Haque
 
Advanced SQL Webinar
Advanced SQL WebinarAdvanced SQL Webinar
Advanced SQL WebinarRam Kedem
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Trainingbixxman
 
ALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSgaurav koriya
 
Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Achmad Solichin
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commandsBelle Wx
 
Sql Basics | Edureka
Sql Basics | EdurekaSql Basics | Edureka
Sql Basics | EdurekaEdureka!
 
Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Achmad Solichin
 

Mais procurados (16)

06.01 sql select distinct
06.01 sql select distinct06.01 sql select distinct
06.01 sql select distinct
 
Structure query language (sql)
Structure query language (sql)Structure query language (sql)
Structure query language (sql)
 
Complete Sql Server querries
Complete Sql Server querriesComplete Sql Server querries
Complete Sql Server querries
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Sql
SqlSql
Sql
 
Advanced SQL Webinar
Advanced SQL WebinarAdvanced SQL Webinar
Advanced SQL Webinar
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
ALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMS
 
Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)
 
BIS05 Introduction to SQL
BIS05 Introduction to SQLBIS05 Introduction to SQL
BIS05 Introduction to SQL
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
Sql Basics | Edureka
Sql Basics | EdurekaSql Basics | Edureka
Sql Basics | Edureka
 
Excel tutorial
Excel tutorialExcel tutorial
Excel tutorial
 
Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)
 
Sql basics v2
Sql basics v2Sql basics v2
Sql basics v2
 

Destaque

Từ điển nhiếp ảnh
Từ điển nhiếp ảnh Từ điển nhiếp ảnh
Từ điển nhiếp ảnh Tran Tuan
 
7 angeliukai
7 angeliukai7 angeliukai
7 angeliukaiManobatai
 
Jan Zając - Psychologiczne i społeczne mechanizmy blogowania
Jan Zając - Psychologiczne i społeczne mechanizmy blogowaniaJan Zając - Psychologiczne i społeczne mechanizmy blogowania
Jan Zając - Psychologiczne i społeczne mechanizmy blogowaniaKrystian Cieślak
 
100614 nhung niem vui nho
100614 nhung niem vui nho100614 nhung niem vui nho
100614 nhung niem vui nhoTran Tuan
 
100510 ong chau va con lua
100510 ong chau va con lua100510 ong chau va con lua
100510 ong chau va con luaTran Tuan
 
100620.dieu uoc cuoi cung
100620.dieu uoc cuoi cung100620.dieu uoc cuoi cung
100620.dieu uoc cuoi cungTran Tuan
 
101223 song tich cuc
101223 song tich cuc101223 song tich cuc
101223 song tich cucTran Tuan
 
Marta Olcoń Kubicka - Kulturowe wymiary blogowania
Marta Olcoń Kubicka - Kulturowe wymiary blogowaniaMarta Olcoń Kubicka - Kulturowe wymiary blogowania
Marta Olcoń Kubicka - Kulturowe wymiary blogowaniaKrystian Cieślak
 
Group 1 powerpoint
Group 1 powerpointGroup 1 powerpoint
Group 1 powerpointNadine Cobo
 
Benh vien cua chua
Benh vien cua chuaBenh vien cua chua
Benh vien cua chuaTran Tuan
 
101707 chuyen con kien
101707 chuyen con kien101707 chuyen con kien
101707 chuyen con kienTran Tuan
 
Sami Ben Gharbia - State of free speech in world blogosphere
Sami Ben Gharbia - State of free speech in world blogosphereSami Ben Gharbia - State of free speech in world blogosphere
Sami Ben Gharbia - State of free speech in world blogosphereKrystian Cieślak
 
100818 chia khoa niem vui
100818 chia khoa niem vui100818 chia khoa niem vui
100818 chia khoa niem vuiTran Tuan
 

Destaque (16)

Từ điển nhiếp ảnh
Từ điển nhiếp ảnh Từ điển nhiếp ảnh
Từ điển nhiếp ảnh
 
7 angeliukai
7 angeliukai7 angeliukai
7 angeliukai
 
Jan Zając - Psychologiczne i społeczne mechanizmy blogowania
Jan Zając - Psychologiczne i społeczne mechanizmy blogowaniaJan Zając - Psychologiczne i społeczne mechanizmy blogowania
Jan Zając - Psychologiczne i społeczne mechanizmy blogowania
 
Pepsi light
Pepsi lightPepsi light
Pepsi light
 
Jnvst 2014 appform
Jnvst 2014 appformJnvst 2014 appform
Jnvst 2014 appform
 
100614 nhung niem vui nho
100614 nhung niem vui nho100614 nhung niem vui nho
100614 nhung niem vui nho
 
Mimer sql realtime
Mimer sql realtimeMimer sql realtime
Mimer sql realtime
 
100510 ong chau va con lua
100510 ong chau va con lua100510 ong chau va con lua
100510 ong chau va con lua
 
100620.dieu uoc cuoi cung
100620.dieu uoc cuoi cung100620.dieu uoc cuoi cung
100620.dieu uoc cuoi cung
 
101223 song tich cuc
101223 song tich cuc101223 song tich cuc
101223 song tich cuc
 
Marta Olcoń Kubicka - Kulturowe wymiary blogowania
Marta Olcoń Kubicka - Kulturowe wymiary blogowaniaMarta Olcoń Kubicka - Kulturowe wymiary blogowania
Marta Olcoń Kubicka - Kulturowe wymiary blogowania
 
Group 1 powerpoint
Group 1 powerpointGroup 1 powerpoint
Group 1 powerpoint
 
Benh vien cua chua
Benh vien cua chuaBenh vien cua chua
Benh vien cua chua
 
101707 chuyen con kien
101707 chuyen con kien101707 chuyen con kien
101707 chuyen con kien
 
Sami Ben Gharbia - State of free speech in world blogosphere
Sami Ben Gharbia - State of free speech in world blogosphereSami Ben Gharbia - State of free speech in world blogosphere
Sami Ben Gharbia - State of free speech in world blogosphere
 
100818 chia khoa niem vui
100818 chia khoa niem vui100818 chia khoa niem vui
100818 chia khoa niem vui
 

Semelhante a Sql basics (20)

Sql General
Sql General Sql General
Sql General
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Sql 2006
Sql 2006Sql 2006
Sql 2006
 
Sql
SqlSql
Sql
 
Query
QueryQuery
Query
 
Sql
SqlSql
Sql
 
Hira
HiraHira
Hira
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 
SQL Beginners anishurrehman.cloud.pdf
SQL Beginners anishurrehman.cloud.pdfSQL Beginners anishurrehman.cloud.pdf
SQL Beginners anishurrehman.cloud.pdf
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 
Sql functions
Sql functionsSql functions
Sql functions
 
Dbms
DbmsDbms
Dbms
 
SQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptxSQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptx
 
SQL Query
SQL QuerySQL Query
SQL Query
 
Lecture5-SQL.docx
Lecture5-SQL.docxLecture5-SQL.docx
Lecture5-SQL.docx
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
 
SQL
SQLSQL
SQL
 

Último

Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
CHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxCHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxAneriPatwari
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptxAneriPatwari
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Celine George
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17Celine George
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 

Último (20)

Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
CHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxCHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptx
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 

Sql basics

  • 2. SQL – What Is It? Structured Query Language Common Language For Variety of Databases ANSI Standard BUT…. Two Types of SQL DML – Data Manipulation Language (SELECT) DDL – Data Definition Language (CREATE TABLE)
  • 3. Where To Use SQL*Plus TOAD SQL Navigator ODBC Supported Connections Excel Access Lotus 1-2-3 Heart of PL/SQL
  • 4. Pros & Cons of SQL Pros: Very flexible Universal (Oracle, Access, Paradox, etc) Relatively Few Commands to Learn Cons: Requires Detailed Knowledge of the Structure of the Database Can Provide Misleading Results
  • 5. Basic SQL Components SELECT schema.table.column FROM table alias WHERE [conditions] ORDER BY [columns] ; Defines the end of an SQL statement Some programs require it, some do not (TOAD Does Not) Needed only if multiple SQL statements run in a script Optional Elements
  • 6. SELECT Statement SELECT Statement Defines WHAT is to be returned (separated by commas) Database Columns (From Tables or Views) Constant Text Values Formulas Pre-defined Functions Group Functions (COUNT, SUM, MAX, MIN, AVG) “*” Mean All Columns From All Tables In the FROM Statement Example: SELECT state_code, state_name
  • 7. FROM Statement Defines the Table(s) or View(s) Used by the SELECT or WHERE Statements You MUST Have a FROM statement Multiple Tables/Views are separated by Commas
  • 8. Examples SELECT state_name, state_abbr FROM states SELECT * FROM agencies SELECT arithmetic_mean – minimum_value FROM annual_summaries
  • 9. WHERE Clause Optional Defines what records are to be included in the query Uses Conditional Operators =, >, >=, <, <=, != (<>) BETWEEN x AND y IN (list) LIKE ‘%string’ (“%” is a wild-card) IS NULL NOT {BETWEEN / IN / LIKE / NULL} Multiple Conditions Linked with AND & OR Statements Strings Contained Within SINGLE QUOTES!!
  • 10. AND & OR Multiple WHERE conditions are Linked by AND / OR Statements “AND” Means All Conditions are TRUE for the Record “OR” Means at least 1 of the Conditions is TRUE You May Group Statements with ( ) BE CAREFUL MIXING “AND” & “OR” Conditions
  • 11. Examples with WHERE SELECT * FROM annual_summaries WHERE sd_duration_code = ‘1’ SELECT state_name FROM states WHERE state_population > 15000000
  • 12. More Examples SELECT state_name, state_population FROM states WHERE state_name LIKE ‘%NORTH%’ SELECT * FROM annual_summaries WHERE sd_duration_code IN (‘1’, ‘W’, ‘X’) AND annual_summary_year = 2000
  • 13. Be Careful! SELECT mo_mo_id, sd_duration_code FROM annual_summaries WHERE annual_summary_year = 2003 AND values_gt_pri_std > 0 OR values_gt_sec_std > 0 SELECT mo_mo_id, sd_duration_code FROM annual_summaries WHERE annual_summary_year = 2003 AND (values_gt_pri_std > 0 OR values_gt_sec_std > 0)
  • 14. ORDER BY Statement Defines How the Records are to be Sorted Must be in the SELECT statement to be ORDER BY Default is to order in ASC (Ascending) order Can Sort in Reverse (Descending) Order with “DESC” After the Column Name
  • 15. ORDER BY Example SELECT * FROM agencies ORDER BY agency_desc SELECT cc_cn_stt_state_code, site_id FROM sites WHERE lut_land_use_type = ‘MOBILE’ ORDER BY cc_cn_stt_state_code DESC
  • 16. Group Functions Performs Common Mathematical Operations on a Group of Records Must define what Constitutes a Group by Using the GROUP BY Clause All non-Group elements in the SELECT Statement Must be in the GROUP BY Clause (Additional Columns are Optional)
  • 17. Group By Example SELECT si_si_id, COUNT(mo_id) FROM monitors GROUP BY si_si_id SELECT AVG(max_sample_value) FROM summary_maximums WHERE max_level <= 3 AND max_ind = ‘REG’ GROUP BY ans_ans_id
  • 18. OK, I understand How to Get Data From 1 Table… What about Multiple Tables? V_MONITOR_ID MONITORS PARAMETERS PARAMETER_CODE PARAMETER_DESC MO_ID SI_SI_ID PA_PARAMETER_CODE POC MO_ID AIRS_MONITOR_ID STATE_CODE COUNTY_CODE SITE_ID PARAMETER_CODE POC
  • 19. Primary & Foreign Keys Primary Keys 1 or More Columns Used to Uniquely Identify a record. All Columns Defined as PK’s MUST be populated Foreign Keys Value on a table that references a Primary Key from a different table
  • 20. Primary & Foreign Keys SITES SI_ID% SITE_LATITUDE SITE_LONGITUDE STREET_ADDRESS PARAMETERS PARAMETER_CODE% PARAMETER_DESC V_MONITOR_ID MONITORS MO_ID% SI_SI_ID* PA_PARAMETER_CODE* POC * = Foreign Key % = Primary Key MO_ID STATE_CODE COUNTY_CODE SITE_ID PARAMETER_CODE POC
  • 22. Cartesian Join / Simple Join SELECT mo_id, poc, parameter_desc FROM monitors, parameters MONITORS PARAMETERS MO_ID SI_SI_ID PA_PARAMETER_CODE POC Parameter_Code Parameter_Desc 1 1 44201 1 44201 Ozone 2 1 42101 1 3 1 42101 2 4 2 81102 1 42101 CO 42401 SO2 5 2 44201 1 81102 PM10 6 3 42401 1
  • 23. Joining Tables SELECT mo_id, poc, parameter_desc FROM monitors, parameters WHERE pa_parameter_code = parameter_code MONITORS PARAMETERS MO_ID SI_SI_ID PA_PARAMETER_CODE POC Parameter_Code Parameter_Desc 1 1 44201 1 44201 Ozone 2 1 42101 1 3 1 42101 2 4 2 81102 1 42101 CO 42401 SO2 5 2 44201 1 81102 PM10 6 3 42401 1
  • 24. Joining Tables Joins Between Tables are Usually Based on Primary / Foreign Keys Make Sure Joins Between All Tables in the FROM Clause Exist List Joins Between Tables Before Other Selection Elements
  • 25. Aliases “Shorthand” for Table or Column References SELECT Aliases Appear as Column Headers in the Output Aliases Cannot be Keywords
  • 26. Previous SQL With Aliases SELECT mo.mo_id, mo.poc, pa.parameter_desc parameter FROM monitors mo, parameters pa WHERE mo.pa_parameter_code = pa.parameter_code
  • 27. Why Use an Alias? Saves Typing Good Internal Documentation Better Headers If the same column name exists on multiple tables, SQL needs a way to know which element you are referencing (MO_MO_ID for example)
  • 28. Recap Basic Structural Elements SELECT FROM WHERE ORDER BY GROUP BY Selecting From Multiple Tables Join Multiple Tables via Primary & Foreign Keys Aliases