SlideShare a Scribd company logo
1 of 22
SQL for Business Analysts
Why BAs use SQL
1. Creating an ad hoc report or other business
intelligence work (i.e., analysis that may drive
business decisions) .
2. Gathering and documenting requirements for a
new report to be developed.
3. Screen Design – You need a screen shot/grab for
a specific scenario so that you can edit it for a
functional requirements document.
4. Research – proving or disproving that certain
data exists or does not exist. Discovering how
much data exists (e.g., how many customers
have account balances over $100k).
What is SQL?
• SQL is Structured Query Language.
• SQL is the standard language used to write queries which are in
turn used to extract information from relational databases.
• We use SQL to ask a DBMS Database management System for
information. The DBMS reads it, like a message, and then returns
what we requested (not what we need or want).
• Each DBMS (e.g., Oracle, SQL Server, Access) uses a variation of
SQL, but most are very similar. Access has the most differences.
• SQL is structured which means it has certain rules and things are
done in a certain order. The DBMS reads it from the top down.
• SQL, by itself, is not a programming language.
• SQL is comprised of key/reserved words. Some are required
(must use them) and others are optional (use at your discretion).
• SQL is NOT case sensitive.
Data vs. Information
• Data is unstructured and unorganized.
• Information is created by structuring and
organizing data.
• A good query or report should provide
information, not just data. Decision makers
need information to make good decisions.
Lego Data Demo
• The loose pieces of Lego represent different
types of data. Examples include:
• Text
• Dates
• Numbers
• In this state, the data has no meaning, no
context, and cannot be used.
Lego Information Demo
• When the data is organized and put into
fields in a database table, it then has
meaning, context, and can be queried.
• Example, if the data were pieces of
customer Orders, we might put them into
the Orders or Sales table with fields:
• dates into the Order_Date field
• numbers in the Order_Total and/or Price fields.
• text into the Notes or special handling fields.
Lego Query Demo
• The data is now transformed into information and
can be queried.
• A query is a question to the database.
• The DBMS will read your question (query) and
return an answer.
• We often call the “answer” to our query a result set.
• The ultimate answer to the ultimate question is 42.
Types of SQL Queries
• Basic Select Queries – used to query/retrieve information from a
table (only performs a read operation on a database).
• Update Queries – these changes data/information in existing
records (performs a database write operation).
• Insert Queries – adds new records/rows to a table
(performs a database write operation).
• Delete Queries – deletes/removes existing records/rows from a
table (performs a database write operation).
• Today, we will only cover Select queries.
• FYI, there is often more than one way to write a Select query to
obtain the same result (i.e., multiple path to reach the same
destination). We will explore some examples of this.
The Parts of Select Queries
• SELECT fields (which columns?)
• FROM table (fields exist in tables in DBs)
• WHERE (optional - filtering criteria at the
row/record level)
• GROUP BY (optional - used to summarize)
• HAVING BY (optional - filtering at the
group/summary level)
• ORDER BY (optional - sorting)
SFWGHO – Remember it using –
Sally Field Wins Green Hairy Oscars.
(actually, Academy Awards are only green and hairy if won by Elton John ;) )
Select Clause
• List field/column names separated by
commas.
• Must use at least one field.
• Use * to view all fields in the table.
• Can also use aliases instead of the exact,
official field’s name.
From Clause
• List table name(s).
• At least one table is required.
• If you use multiple tables, be sure to use a
join (covered in more detail later) between
tables to avoid a Cartesian product.
Where Clause
• The Where clause limits the amount of information that is
returned in the query result set, by set filtering
criteria/conditions.
• The data/information must meet the conditions of the
where clause to be included in the query result set. If the
data does not meet the conditions then it is essentially
excluded.
• You may not want to use a where clause on very small,
reference (a.k.a., lookup) tables, but you should almost
always plan to use a where clause on larger tables.
• Where clause conditions/filters are set using comparison
operators such as >, <, =, like, not, between etc.
Where Clause - Comparison Operators
• Math operators (<, >, =: usually used on numbers and
dates, but can also be used on text.
• Example 1: Total >= 100
• Example 2: Date_Modified > “7/1/2012”
• Between: Commonly used with dates
• Example: Date_Created between #1/1/2012# and
#12/31/2012#.
• Like: usually used with text (a.k.a., strings) and wild card
symbols (e.g., *, _, ?)
• In: use this when you are looking for a multiple things in a
list that are not consecutive (e.g., number in [2,5,7,12])
Example Simple Select Queries
• SELECT Product_Name FROM Table_Product;
(returns all records/rows in the table).
• SELECT Last_Name FROM Table_Customer
WHERE State = “CA”;
• SELECT Order _ID, Order_Total FROM
Table_Orders WHERE Total > 100 ORDER BY
Total;
SQL Demo
• Live Demonstration in Access
• Live Demonstration online at
http://www.w3schools.com/sql/default.asp
SQL Joins (time permitting)
• Joins allow the user to get information from two or
more tables (also views and other queries) without
receiving duplicate or redundant records.
• A Join is made between exactly two tables on
fields that are the same data type and contain the
same information. That is, join a text field to
another text field or a number to number field, date
to date, and so on.
• The syntax for writing a join varies somewhat
between DBMS, especially between Access and
others.
SQL Joins continued
• If you need to join more than two tables, you must have
multiple joins. For example: If you need to use three (3)
tables in a query, you would want at least two joins Table
A joins Table B and Table B joins Table C.
• You can have more than one join between one pair of
tables. For example, Table A Field 1 joins Table B Field 1
and Table A Field 2 joins Table B Field 2. Like two ships
connecting two lines – one is for fuel and the other is for
drinking water. They are separate and you don’t want to
mix the two!
Why use Joins?
• Information is stored in different tables in relational databases. We
often need related information that exists in two or more tables. A
joins allows us to relate/join two tables (regardless of whether the
tables have a database relation) and retrieve only the number of
records we need – no more no less.
• If we query two tables without joining those tables, we will get a
Cartesian Product – the number of records/rows in Table 1
multiplied by the number of records in Table 2. For example:
Table 1 has 1000 records and Table 2 has 2000 records. A query
with no join and no where clause would return 2,000,000 records.
If you add Table 3 which has 3,000 records, the Cartesian product
would be 6,000,000,000 records. This is bad, not good. It ‘s kind
of like “crossing the streams” bad in Ghost Busters.
Join Types
• Inner Join: (probably the most common type and
the easiest to write). This type is no direction and
will only allow records that are in common between
the two tables.
• Outer Join (right and left): This is used when you
need to allow records from one table but only
common records from the other table. This type of
join is directional and specifies which table will be
limited to only records that are in common.
• Self Join: Joins a table to itself for special purposes.
Join Examples
• Inner Joins:
• Inner Join Syntax: SELECT Order_ID, Product_Name FROM Products
INNER JOIN Orders ON Products.Product_ID = Orders.Production_ID
WHERE Product_Category = “Tools”;
• Where Clause Syntax: SELECT Order_ID, Product_Name FROM Products,
Orders WHERE Products.Product_ID = Orders.Production_ID AND
Product_Category = “Tools”;
• Outer Joins:
• Inner Join Syntax: SELECT Order_ID, Product_Name FROM Products LEFT
JOIN Orders ON Products.Product_ID = Orders.Production_ID WHERE
Product_Category = “Tools”;
• Where Clause Syntax: SELECT Order_ID, Product_Name FROM Products,
Orders WHERE Products.Product_ID = Orders.Production_ID AND
Product_Category = “Tools”;
SQL Join Demo
• Live Demonstration of creating Select
Queries with Joins using Access
Recommended Reading
• Sam’s Teach Yourself SQL in 10 Minutes
• Access 2007 Pure SQL

More Related Content

What's hot

Introduction of sql server indexing
Introduction of sql server indexingIntroduction of sql server indexing
Introduction of sql server indexingMahabubur Rahaman
 
Chapter 4 microsoft access 2010
Chapter 4 microsoft access 2010Chapter 4 microsoft access 2010
Chapter 4 microsoft access 2010home
 
Indexes: The Second Pillar of Database Wisdom
Indexes: The Second Pillar of Database WisdomIndexes: The Second Pillar of Database Wisdom
Indexes: The Second Pillar of Database Wisdomgisborne
 
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1 "Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1 Andriy Krayniy
 
Database Essentials for Healthcare Finance Professionals
Database Essentials for Healthcare Finance ProfessionalsDatabase Essentials for Healthcare Finance Professionals
Database Essentials for Healthcare Finance ProfessionalsBrad Adams
 
2019 02 21_biological_databases_part2_v_upload
2019 02 21_biological_databases_part2_v_upload2019 02 21_biological_databases_part2_v_upload
2019 02 21_biological_databases_part2_v_uploadProf. Wim Van Criekinge
 
Introductionto excel2007
Introductionto excel2007Introductionto excel2007
Introductionto excel2007Khan Rahimeen
 
SQL Server Index and Partition Strategy
SQL Server Index and Partition StrategySQL Server Index and Partition Strategy
SQL Server Index and Partition StrategyHamid J. Fard
 
Sql server ___________session_17(indexes)
Sql server  ___________session_17(indexes)Sql server  ___________session_17(indexes)
Sql server ___________session_17(indexes)Ehtisham Ali
 
Database Application for La Salle
Database Application for La SalleDatabase Application for La Salle
Database Application for La SalleJimmy Chu
 

What's hot (19)

Cis145 Final Review
Cis145 Final ReviewCis145 Final Review
Cis145 Final Review
 
Introduction of sql server indexing
Introduction of sql server indexingIntroduction of sql server indexing
Introduction of sql server indexing
 
Chapter 4 microsoft access 2010
Chapter 4 microsoft access 2010Chapter 4 microsoft access 2010
Chapter 4 microsoft access 2010
 
Indexes: The Second Pillar of Database Wisdom
Indexes: The Second Pillar of Database WisdomIndexes: The Second Pillar of Database Wisdom
Indexes: The Second Pillar of Database Wisdom
 
Access 4 U
Access 4 UAccess 4 U
Access 4 U
 
Query basics
Query basicsQuery basics
Query basics
 
Cis145 Final Review
Cis145 Final ReviewCis145 Final Review
Cis145 Final Review
 
CIS145 Final Review
CIS145 Final ReviewCIS145 Final Review
CIS145 Final Review
 
CIS 145 test 1 review
CIS 145 test 1 reviewCIS 145 test 1 review
CIS 145 test 1 review
 
Description of data
Description of dataDescription of data
Description of data
 
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1 "Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
 
Database Essentials for Healthcare Finance Professionals
Database Essentials for Healthcare Finance ProfessionalsDatabase Essentials for Healthcare Finance Professionals
Database Essentials for Healthcare Finance Professionals
 
2019 02 21_biological_databases_part2_v_upload
2019 02 21_biological_databases_part2_v_upload2019 02 21_biological_databases_part2_v_upload
2019 02 21_biological_databases_part2_v_upload
 
Introductionto excel2007
Introductionto excel2007Introductionto excel2007
Introductionto excel2007
 
Index Tuning
Index TuningIndex Tuning
Index Tuning
 
SQL Server Index and Partition Strategy
SQL Server Index and Partition StrategySQL Server Index and Partition Strategy
SQL Server Index and Partition Strategy
 
Sql server ___________session_17(indexes)
Sql server  ___________session_17(indexes)Sql server  ___________session_17(indexes)
Sql server ___________session_17(indexes)
 
Analysis 3 1
Analysis 3 1Analysis 3 1
Analysis 3 1
 
Database Application for La Salle
Database Application for La SalleDatabase Application for La Salle
Database Application for La Salle
 

Viewers also liked

Daudzdzīvokļu un sabiedrisko ēku enerģijas patēriņa modelēšana, izmantojot di...
Daudzdzīvokļu un sabiedrisko ēku enerģijas patēriņa modelēšana, izmantojot di...Daudzdzīvokļu un sabiedrisko ēku enerģijas patēriņa modelēšana, izmantojot di...
Daudzdzīvokļu un sabiedrisko ēku enerģijas patēriņa modelēšana, izmantojot di...Ekonomikas ministrija/ Dzīvo siltāk
 
Ārējo norobežojošo konstrukciju atjaunošanas iespējas. Apmestās fasādes
Ārējo norobežojošo konstrukciju atjaunošanas iespējas. Apmestās fasādesĀrējo norobežojošo konstrukciju atjaunošanas iespējas. Apmestās fasādes
Ārējo norobežojošo konstrukciju atjaunošanas iespējas. Apmestās fasādesEkonomikas ministrija/ Dzīvo siltāk
 
Dzīvokļu maksājamās daļas noteikšana, izmantojot individuālo siltumenerģijas ...
Dzīvokļu maksājamās daļas noteikšana, izmantojot individuālo siltumenerģijas ...Dzīvokļu maksājamās daļas noteikšana, izmantojot individuālo siltumenerģijas ...
Dzīvokļu maksājamās daļas noteikšana, izmantojot individuālo siltumenerģijas ...Ekonomikas ministrija/ Dzīvo siltāk
 
FIWARE: the best is yet to come
FIWARE: the best is yet to comeFIWARE: the best is yet to come
FIWARE: the best is yet to comeFIWARE
 
DAY 2 - Starting in Photoshop (Images and Layers)
DAY 2 - Starting in Photoshop (Images and Layers)DAY 2 - Starting in Photoshop (Images and Layers)
DAY 2 - Starting in Photoshop (Images and Layers)Sef Cambaliza
 
Latgales plānošanas reģiona stāsts par reģiona pašvaldību piedāvājumu uzņēmēj...
Latgales plānošanas reģiona stāsts par reģiona pašvaldību piedāvājumu uzņēmēj...Latgales plānošanas reģiona stāsts par reģiona pašvaldību piedāvājumu uzņēmēj...
Latgales plānošanas reģiona stāsts par reģiona pašvaldību piedāvājumu uzņēmēj...Ekonomikas ministrija
 

Viewers also liked (10)

Ieteikumi kvalitatīvu energoauditu sagatavošanai
Ieteikumi  kvalitatīvu energoauditu  sagatavošanai Ieteikumi  kvalitatīvu energoauditu  sagatavošanai
Ieteikumi kvalitatīvu energoauditu sagatavošanai
 
Daudzdzīvokļu un sabiedrisko ēku enerģijas patēriņa modelēšana, izmantojot di...
Daudzdzīvokļu un sabiedrisko ēku enerģijas patēriņa modelēšana, izmantojot di...Daudzdzīvokļu un sabiedrisko ēku enerģijas patēriņa modelēšana, izmantojot di...
Daudzdzīvokļu un sabiedrisko ēku enerģijas patēriņa modelēšana, izmantojot di...
 
Būvju tehniskā apsekošana un atzinuma sagatavošana
Būvju tehniskā apsekošana un atzinuma sagatavošanaBūvju tehniskā apsekošana un atzinuma sagatavošana
Būvju tehniskā apsekošana un atzinuma sagatavošana
 
Tehniskās dokumentācijas sagatavošana
Tehniskās dokumentācijas sagatavošanaTehniskās dokumentācijas sagatavošana
Tehniskās dokumentācijas sagatavošana
 
Būvju tehniskā apsekošana un atzinuma sagatavošana
Būvju tehniskā apsekošana un atzinuma sagatavošanaBūvju tehniskā apsekošana un atzinuma sagatavošana
Būvju tehniskā apsekošana un atzinuma sagatavošana
 
Ārējo norobežojošo konstrukciju atjaunošanas iespējas. Apmestās fasādes
Ārējo norobežojošo konstrukciju atjaunošanas iespējas. Apmestās fasādesĀrējo norobežojošo konstrukciju atjaunošanas iespējas. Apmestās fasādes
Ārējo norobežojošo konstrukciju atjaunošanas iespējas. Apmestās fasādes
 
Dzīvokļu maksājamās daļas noteikšana, izmantojot individuālo siltumenerģijas ...
Dzīvokļu maksājamās daļas noteikšana, izmantojot individuālo siltumenerģijas ...Dzīvokļu maksājamās daļas noteikšana, izmantojot individuālo siltumenerģijas ...
Dzīvokļu maksājamās daļas noteikšana, izmantojot individuālo siltumenerģijas ...
 
FIWARE: the best is yet to come
FIWARE: the best is yet to comeFIWARE: the best is yet to come
FIWARE: the best is yet to come
 
DAY 2 - Starting in Photoshop (Images and Layers)
DAY 2 - Starting in Photoshop (Images and Layers)DAY 2 - Starting in Photoshop (Images and Layers)
DAY 2 - Starting in Photoshop (Images and Layers)
 
Latgales plānošanas reģiona stāsts par reģiona pašvaldību piedāvājumu uzņēmēj...
Latgales plānošanas reģiona stāsts par reģiona pašvaldību piedāvājumu uzņēmēj...Latgales plānošanas reģiona stāsts par reģiona pašvaldību piedāvājumu uzņēmēj...
Latgales plānošanas reģiona stāsts par reģiona pašvaldību piedāvājumu uzņēmēj...
 

Similar to SQL_Part1

Data Base Management System.pdf
Data Base Management System.pdfData Base Management System.pdf
Data Base Management System.pdfTENZING LHADON
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management SystemMian Abdul Raheem
 
Database indexing techniques
Database indexing techniquesDatabase indexing techniques
Database indexing techniquesahmadmughal0312
 
Implementing the Databese Server session 02
Implementing the Databese Server session 02Implementing the Databese Server session 02
Implementing the Databese Server session 02Guillermo Julca
 
SQL SERVER Training in Pune Slides
SQL SERVER Training in Pune SlidesSQL SERVER Training in Pune Slides
SQL SERVER Training in Pune Slidesenosislearningcom
 
BI Knowledge Sharing Session 2
BI Knowledge Sharing Session 2BI Knowledge Sharing Session 2
BI Knowledge Sharing Session 2Kelvin Chan
 
Database Management System
Database Management SystemDatabase Management System
Database Management SystemMuhd Dembo
 
microsoft_access_working_with_forms_and_generating_reports__107.ppt
microsoft_access_working_with_forms_and_generating_reports__107.pptmicrosoft_access_working_with_forms_and_generating_reports__107.ppt
microsoft_access_working_with_forms_and_generating_reports__107.pptKunle Faseyi
 
Query Optimization in SQL Server
Query Optimization in SQL ServerQuery Optimization in SQL Server
Query Optimization in SQL ServerRajesh Gunasundaram
 
Introduction to SQL (for Chicago Booth MBA technology club)
Introduction to SQL (for Chicago Booth MBA technology club)Introduction to SQL (for Chicago Booth MBA technology club)
Introduction to SQL (for Chicago Booth MBA technology club)Jennifer Berk
 
Relational Database Language.pptx
Relational Database Language.pptxRelational Database Language.pptx
Relational Database Language.pptxSheethal Aji Mani
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowPavithSingh
 

Similar to SQL_Part1 (20)

Data Base Management System.pdf
Data Base Management System.pdfData Base Management System.pdf
Data Base Management System.pdf
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management System
 
Sql introduction
Sql introductionSql introduction
Sql introduction
 
Database Basics
Database BasicsDatabase Basics
Database Basics
 
Database indexing techniques
Database indexing techniquesDatabase indexing techniques
Database indexing techniques
 
MS-ACCESS.pptx
MS-ACCESS.pptxMS-ACCESS.pptx
MS-ACCESS.pptx
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
 
Implementing the Databese Server session 02
Implementing the Databese Server session 02Implementing the Databese Server session 02
Implementing the Databese Server session 02
 
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
 
SQL SERVER Training in Pune Slides
SQL SERVER Training in Pune SlidesSQL SERVER Training in Pune Slides
SQL SERVER Training in Pune Slides
 
BI Knowledge Sharing Session 2
BI Knowledge Sharing Session 2BI Knowledge Sharing Session 2
BI Knowledge Sharing Session 2
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
 
microsoft_access_working_with_forms_and_generating_reports__107.ppt
microsoft_access_working_with_forms_and_generating_reports__107.pptmicrosoft_access_working_with_forms_and_generating_reports__107.ppt
microsoft_access_working_with_forms_and_generating_reports__107.ppt
 
Query Optimization in SQL Server
Query Optimization in SQL ServerQuery Optimization in SQL Server
Query Optimization in SQL Server
 
Access 03
Access 03Access 03
Access 03
 
Introduction to SQL (for Chicago Booth MBA technology club)
Introduction to SQL (for Chicago Booth MBA technology club)Introduction to SQL (for Chicago Booth MBA technology club)
Introduction to SQL (for Chicago Booth MBA technology club)
 
Introduction to ms access database
Introduction to ms access databaseIntroduction to ms access database
Introduction to ms access database
 
Access 2010
Access 2010Access 2010
Access 2010
 
Relational Database Language.pptx
Relational Database Language.pptxRelational Database Language.pptx
Relational Database Language.pptx
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
 

SQL_Part1

  • 1. SQL for Business Analysts
  • 2. Why BAs use SQL 1. Creating an ad hoc report or other business intelligence work (i.e., analysis that may drive business decisions) . 2. Gathering and documenting requirements for a new report to be developed. 3. Screen Design – You need a screen shot/grab for a specific scenario so that you can edit it for a functional requirements document. 4. Research – proving or disproving that certain data exists or does not exist. Discovering how much data exists (e.g., how many customers have account balances over $100k).
  • 3. What is SQL? • SQL is Structured Query Language. • SQL is the standard language used to write queries which are in turn used to extract information from relational databases. • We use SQL to ask a DBMS Database management System for information. The DBMS reads it, like a message, and then returns what we requested (not what we need or want). • Each DBMS (e.g., Oracle, SQL Server, Access) uses a variation of SQL, but most are very similar. Access has the most differences. • SQL is structured which means it has certain rules and things are done in a certain order. The DBMS reads it from the top down. • SQL, by itself, is not a programming language. • SQL is comprised of key/reserved words. Some are required (must use them) and others are optional (use at your discretion). • SQL is NOT case sensitive.
  • 4. Data vs. Information • Data is unstructured and unorganized. • Information is created by structuring and organizing data. • A good query or report should provide information, not just data. Decision makers need information to make good decisions.
  • 5. Lego Data Demo • The loose pieces of Lego represent different types of data. Examples include: • Text • Dates • Numbers • In this state, the data has no meaning, no context, and cannot be used.
  • 6. Lego Information Demo • When the data is organized and put into fields in a database table, it then has meaning, context, and can be queried. • Example, if the data were pieces of customer Orders, we might put them into the Orders or Sales table with fields: • dates into the Order_Date field • numbers in the Order_Total and/or Price fields. • text into the Notes or special handling fields.
  • 7. Lego Query Demo • The data is now transformed into information and can be queried. • A query is a question to the database. • The DBMS will read your question (query) and return an answer. • We often call the “answer” to our query a result set. • The ultimate answer to the ultimate question is 42.
  • 8. Types of SQL Queries • Basic Select Queries – used to query/retrieve information from a table (only performs a read operation on a database). • Update Queries – these changes data/information in existing records (performs a database write operation). • Insert Queries – adds new records/rows to a table (performs a database write operation). • Delete Queries – deletes/removes existing records/rows from a table (performs a database write operation). • Today, we will only cover Select queries. • FYI, there is often more than one way to write a Select query to obtain the same result (i.e., multiple path to reach the same destination). We will explore some examples of this.
  • 9. The Parts of Select Queries • SELECT fields (which columns?) • FROM table (fields exist in tables in DBs) • WHERE (optional - filtering criteria at the row/record level) • GROUP BY (optional - used to summarize) • HAVING BY (optional - filtering at the group/summary level) • ORDER BY (optional - sorting) SFWGHO – Remember it using – Sally Field Wins Green Hairy Oscars. (actually, Academy Awards are only green and hairy if won by Elton John ;) )
  • 10. Select Clause • List field/column names separated by commas. • Must use at least one field. • Use * to view all fields in the table. • Can also use aliases instead of the exact, official field’s name.
  • 11. From Clause • List table name(s). • At least one table is required. • If you use multiple tables, be sure to use a join (covered in more detail later) between tables to avoid a Cartesian product.
  • 12. Where Clause • The Where clause limits the amount of information that is returned in the query result set, by set filtering criteria/conditions. • The data/information must meet the conditions of the where clause to be included in the query result set. If the data does not meet the conditions then it is essentially excluded. • You may not want to use a where clause on very small, reference (a.k.a., lookup) tables, but you should almost always plan to use a where clause on larger tables. • Where clause conditions/filters are set using comparison operators such as >, <, =, like, not, between etc.
  • 13. Where Clause - Comparison Operators • Math operators (<, >, =: usually used on numbers and dates, but can also be used on text. • Example 1: Total >= 100 • Example 2: Date_Modified > “7/1/2012” • Between: Commonly used with dates • Example: Date_Created between #1/1/2012# and #12/31/2012#. • Like: usually used with text (a.k.a., strings) and wild card symbols (e.g., *, _, ?) • In: use this when you are looking for a multiple things in a list that are not consecutive (e.g., number in [2,5,7,12])
  • 14. Example Simple Select Queries • SELECT Product_Name FROM Table_Product; (returns all records/rows in the table). • SELECT Last_Name FROM Table_Customer WHERE State = “CA”; • SELECT Order _ID, Order_Total FROM Table_Orders WHERE Total > 100 ORDER BY Total;
  • 15. SQL Demo • Live Demonstration in Access • Live Demonstration online at http://www.w3schools.com/sql/default.asp
  • 16. SQL Joins (time permitting) • Joins allow the user to get information from two or more tables (also views and other queries) without receiving duplicate or redundant records. • A Join is made between exactly two tables on fields that are the same data type and contain the same information. That is, join a text field to another text field or a number to number field, date to date, and so on. • The syntax for writing a join varies somewhat between DBMS, especially between Access and others.
  • 17. SQL Joins continued • If you need to join more than two tables, you must have multiple joins. For example: If you need to use three (3) tables in a query, you would want at least two joins Table A joins Table B and Table B joins Table C. • You can have more than one join between one pair of tables. For example, Table A Field 1 joins Table B Field 1 and Table A Field 2 joins Table B Field 2. Like two ships connecting two lines – one is for fuel and the other is for drinking water. They are separate and you don’t want to mix the two!
  • 18. Why use Joins? • Information is stored in different tables in relational databases. We often need related information that exists in two or more tables. A joins allows us to relate/join two tables (regardless of whether the tables have a database relation) and retrieve only the number of records we need – no more no less. • If we query two tables without joining those tables, we will get a Cartesian Product – the number of records/rows in Table 1 multiplied by the number of records in Table 2. For example: Table 1 has 1000 records and Table 2 has 2000 records. A query with no join and no where clause would return 2,000,000 records. If you add Table 3 which has 3,000 records, the Cartesian product would be 6,000,000,000 records. This is bad, not good. It ‘s kind of like “crossing the streams” bad in Ghost Busters.
  • 19. Join Types • Inner Join: (probably the most common type and the easiest to write). This type is no direction and will only allow records that are in common between the two tables. • Outer Join (right and left): This is used when you need to allow records from one table but only common records from the other table. This type of join is directional and specifies which table will be limited to only records that are in common. • Self Join: Joins a table to itself for special purposes.
  • 20. Join Examples • Inner Joins: • Inner Join Syntax: SELECT Order_ID, Product_Name FROM Products INNER JOIN Orders ON Products.Product_ID = Orders.Production_ID WHERE Product_Category = “Tools”; • Where Clause Syntax: SELECT Order_ID, Product_Name FROM Products, Orders WHERE Products.Product_ID = Orders.Production_ID AND Product_Category = “Tools”; • Outer Joins: • Inner Join Syntax: SELECT Order_ID, Product_Name FROM Products LEFT JOIN Orders ON Products.Product_ID = Orders.Production_ID WHERE Product_Category = “Tools”; • Where Clause Syntax: SELECT Order_ID, Product_Name FROM Products, Orders WHERE Products.Product_ID = Orders.Production_ID AND Product_Category = “Tools”;
  • 21. SQL Join Demo • Live Demonstration of creating Select Queries with Joins using Access
  • 22. Recommended Reading • Sam’s Teach Yourself SQL in 10 Minutes • Access 2007 Pure SQL