SlideShare a Scribd company logo
1 of 46
Structured Query Language
Brian Foote
Fox Valley Technical College
WELCOME!!!!
What is the plan for today?
• Learn about databases
• Learn about SQL Server
• Learn about SQL
• Whatever else you want to do!
• foote@fvtc.edu
• 920-735-2568
• Skype : brian.d.foote
• Twitter : @footefvtc
About Me
• UW-Eau Claire
• IT Consultant – 18 years
• Cap Gemini, Greenbrier & Russell, Fujitsu,
Skyline, Rush Lake Consulting
• CUNA Mutual Group, Kimberly-Clark, The
Real Estate Group, Bemis, Associated
Bank, JJ Keller, ThedaCare, Oshkosh
Chamber of Oshkosh, Pierce
• Husband and father of 3 daughters.
• Duck Hunter/Basketball Coach
• Teaching – 8th semester
Brian Foote
SSMS – SQL Server Management Studio –
Microsoft’s Query tool to their own database
format.
HOW?
SSMS – SQL SERVER MANAGEMENT STUDIO
Start
All Programs
Microsoft SQL Server
SSMS
CONNECTING TO SQL SERVER
Information you need to connect:
Server
Database
Credentials
SSMS – Connecting to SQL Server
CONNECTING TO SQL
SERVER
SQL is short for Structured Query Language and
is a widely used database language, providing
means of data manipulation (store, retrieve,
update, delete) and database creation.
SQL Server vs SQL?
SQL OVERVIEW
• What is SQL and why is it important?
• Retrieve data from a single table.
• SELECT
• SELECT INTO
• ORDER BY
• WHERE CONDITIONS
• AGGREGATE SELECTS
SQL OVERVIEW
• Retrieve data from a multiple tables.
• JOIN (INNER, LEFT, RIGHT, OUTER)
• SELECT
• SELECT INTO
• ORDER BY
• WHERE CONDITIONS
• AGGREGATE SELECTS
SQL OVERVIEW
SELECT Statement – Used to retrieve data from the database
table.
Syntax :
SELECT Column1, Column2, Column3, *
FROM Table1
Exercise :
• Simple column SELECT
• Simple * SELECT
Example :
SELECT * FROM Customers
SELECT Name, Address. City, State, ZIP
FROM Customers
SQL – SELECT – 1 TABLE
The SQL ORDER BY clause comes in handy when you want to
sort your SQL result sets by some column or columns.
Syntax :
SELECT *
FROM Table1
ORDER BY COLUMN (ORDER BY COLUMN #)
Exercise :
Simple column SELECT ORDER BY
Example :
SELECT * FROM Customers
ORDER BY City, LastName
SQL – SELECT - ORDER
The SQL WHERE clause is used to select data conditionally, by
adding it to already existing SQL SELECT query.
Syntax :
SELECT Column1, Column2, Column3,
FROM Table1
WHERE <CONDITION>
Exercise :
Simple column SELECT WHERE
Example :
SELECT *
FROM Customers
WHERE State = ‘WI’
SQL – SELECT - WHEREHI
The SQL DISTINCT clause is used together with the SQL SELECT
keyword, to return a dataset with unique entries for certain
database table column.
Syntax :
SELECT DISTINCT Column1, Column2, Column3,
INTO Table2
FROM Table1
Exercise :
Simple column SELECT DISTINCT
Example :
SELECT DISTINCT LastName
FROM Customers
SQL – SELECT -
DISTINCT
Condition Must be True or False
Relational Operators
= Equal
<> Not Equal
< Less Than
<= Less Than or Equal To
> Greater than
>= Greater than or Equal To
The SQL WHERE clause is used to select data conditionally, by
adding it to already existing SQL SELECT query.
SQL – SELECT - WHERE
The SQL AND clause is used when you want to specify more than
one condition in your SQL WHERE clause, and at the same time
you want all conditions to be true.
Syntax :
SELECT Column1, Column2, Column3,
FROM Table1
WHERE <CONDITION>
AND <DIFFERENT CONDITION>
Exercise :
Simple column SELECT WHERE AND
Example :
SELECT *
FROM Customers
WHERE State = ‘WI’
AND CITY = ‘MADISON’
SQL – SELECT - AND
The SQL OR statement is used in similar fashion and the major
difference compared to the SQL AND is that OR clause will return
all rows satisfying any of the conditions listed in the WHERE
clause.
Syntax :
SELECT Column1, Column2, Column3,
FROM Table1
WHERE <CONDITION>
OR <DIFFERENT CONDITION>
Exercise :
Simple column SELECT WHERE OR
Example :
SELECT * FROM Customers
WHERE State = ‘WI’ OR State = ‘MN’
SQL – SELECT - OR
Other information
• Be aware or data type.
• String data must in double quotes.
• Numeric data does not.
• Order of precedence.
1. ~ (Bitwise NOT)
2. * (Multiply), / (Division), % (Modulo)
3. + (Positive), - (Negative), + (Add), (+ Concatenate), -
(Subtract)
4. =, >, <, >=, <=, <>, !=, !>, !< (Comparison operators)
5. NOT
6. AND
7. ALL, ANY, BETWEEN, IN, LIKE, OR, SOME
8. = (Assignment)
The SQL WHERE clause is used to select data conditionally, by
adding it to already existing SQL SELECT query.
SQL – SELECT - OOP
BETWEEN - TRUE if the operand is within a range.
test_expression [ NOT ] BETWEEN begin_expression AND
end_expression
EXISTS - TRUE if a sub query contains any rows.
EXISTS {SELECT STATEMENT}
IN - TRUE if the operand is equal to one of a list of expressions.
test_expression [ NOT ] IN ( subquery | expression [ ,...n ] )
NOT - Reverses the value of any other Boolean operator.
[ NOT ] boolean_expression
SQL – SELECT - OTHER
LIKE - TRUE if the operand matches a pattern.
match_expression [ NOT ] LIKE pattern [ ESCAPE escape_character ]
% - Any string of zero or more characters.
(WHERE title LIKE '%computer%' )
_ - Any single character. (WHERE au_fname LIKE '_ean')
[ ] - Any single character within the specified range ([a-f]) or set ([abcdef]).
(WHERE au_lname LIKE '[C-P]arsen' )
[^] - Any single character not within the specified range ([^a-f]) or set
([^abcdef]).
(WHERE au_lname LIKE 'de[^l]%' )
SQL – SELECT - LIKE
The SQL SELECT INTO statement is used to select data from a
SQL database table and to insert it to a different table at the same
time.
Syntax :
SELECT Column1, Column2, Column3,
INTO Table2
FROM Table1
Exercise :
Simple column SELECT INTO
Example :
SELECT Name, Address. City, State, ZIP
INTO NewCustomers
FROM Customers
SELECT * FROM Customers
SQL SELECT INTO
COUNT
MAX
MIN
AVG
SUM
Structured Query Language
Aggregate Operators
SQL - AGGREGATE
The SQL COUNT clause is used together with the SQL SELECT
keyword, to return a dataset with unique entries for certain
database table column.
Syntax :
SELECT COUNT(*)
FROM Table1
Exercise :
Simple column SELECT COUNT
Example :
SELECT COUNT(*)
FROM Customers
SQL – AGGREGATE –
COUNT
The SQL MAX aggregate function allows us to select the highest
(maximum) value for a certain column.
Syntax :
SELECT MAX(Column1)
FROM Table1
Exercise :
SELECT MAX
Example :
SELECT MAX(DOB) AS MaxDOB
FROM Customers
SQL – AGGREGATE - MAX
The SQL MIN aggregate function allows us to select the lowest
(minimum) value for a certain column.
Syntax :
SELECT MIN(Column1)
FROM Table1
Exercise :
SELECT MIN
Example :
SELECT MAX(DOB) AS MinDOB
FROM Customers
SQL – AGGREGATE - MIN
The SQL SUM aggregate function allows selecting the total for a
numeric column.
Syntax :
SELECT SUM(Column1)
FROM Table1
Exercise :
SELECT SUM
Example :
SELECT SUM(SaleAmount) AS TotalSales
FROM Sales
SQL – AGGREGATE - SUM
The SQL AVG aggregate function allows selecting the total for a
numeric column.
Syntax :
SELECT AVG(Column1)
FROM Table1
Exercise :
SELECT AVG
Example :
SELECT AVG(SaleAmount) AS AVGTotalSales
FROM Sales
SQL – AGGREGATE -
AVERAGE
The SQL GROUP BY statement is used along with the SQL
aggregate functions like SUM to provide means of grouping the
result dataset by certain database table column(s).
Syntax :
SELECT COLUMN2, SUM(Column1)
FROM Table1
GROUP BY COLUMN2
Exercise :
SELECT GROUP BY
Example :
SELECT Employee, SUM (Hours)
FROM EmployeeHours
GROUP BY Employee
SQL – AGGREGATE –
GROUP BY
The SQL HAVING clause is used to restrict conditionally the output
of a SQL statement, by a SQL aggregate function used in your
SELECT list of columns.
Syntax :
SELECT COLUMN2, <aggregate function> (Column1)
FROM Table1
GROUP BY COLUMN2
HAVING <aggregate function> (Column1) condition
Exercise :
SELECT HAVING
Example :
SELECT Employee, SUM (Hours)
FROM EmployeeHours
GROUP BY Employee
HAVING SUM (Hours) > 24
SQL – AGGREGATE
HAVING
STRUCTURED QUERY LANGUAGE
JOINS
The SQL JOIN clause is used whenever we have to select data from 2 or
more tables.
Join Types
• INNER - Returns all rows from both tables as long as there is a match
between the columns we are matching On.
• OUTER – Returns rows from the joined tables if they match or not.
The INNER JOIN will
select all rows from both
tables as long as there is
a match between the
columns we are matching
on.
SELECT <select_list>
FROM Table_A A INNER
JOIN Table_B B ON A.Key
= B.Key
SQL – INNER JOIN
LEFT - The LEFT OUTER
JOIN or simply LEFT JOIN
(you can omit the OUTER
keyword in most databases),
selects all the rows from the
first table listed after the FROM
clause, no matter if they have
matches in the second table.
SELECT <select_list> FROM
Table_A A LEFT JOIN Table_B
B ON A.Key = B.Key
SQL – LEFT JOIN
RIGHT - The RIGHT OUTER
JOIN or just RIGHT JOIN
behaves exactly as SQL LEFT
JOIN, except that it returns
all rows from the second
table (the right table in our
SQL JOIN statement).
SELECT <select_list> FROM
Table_A A RIGHT JOIN
Table_B B ON A.Key = B.Key
SQL – RIGHT JOIN
FULL OUTER
-This query will return all
of the records from both
tables, joining records
from the left table (table
A) that match records
from the right table (table
B).
-SELECT <select_list>
FROM Table_A A FULL
OUTER JOIN Table_B B
ON A.Key = B.Key
SQL – FULL OUTER
Left Excluding JOIN
-This query will return all of
the records in the left table
(table A) that do not match
any records in the right
table (table B).
SELECT <select_list>
FROM Table_A A LEFT JOIN
Table_B B ON A.Key =
B.Key WHERE B.Key IS
NULL
SQL – LEFT JOIN
EXCLUDING
Right Excluding JOIN
-This query will return all of
the records in the right
table (table B) that do not
match any records in the
left table (table A).
SELECT <select_list> FROM
Table_A A RIGHT JOIN
Table_B B ON A.Key =
B.Key WHERE A.Key IS
NULL
Outer Excluding JOIN
This query will return all of
the records in the left
table (table A) and all of
the records in the right
table (table B) that do not
match.
SELECT <select_list>
FROM Table_A A FULL
OUTER JOIN Table_B B ON
A.Key = B.Key WHERE
A.Key IS NULL OR B.Key
IS NULL
SQL – OUTER JOIN
EXCLUDING
Structured Query Language
Multiple Tables and Aggregate Operators
• COUNT
• MAX
• MIN
• AVG
• SUM
SQL – AGGREGATE
MULTIPLE TABLES
Structured Query Language
Other Things
• Elimination of Nulls (ISNULL)
• Use of subquerys when the joins just
do not seem right.
• Temp Tables (local (#) and global (##)
• Export to Excel
• Stored Procedures
SQL – OTHER THINGS
SQL – ALL JOINS
Sources
• Microsoft Developer Network - http://msdn.microsoft.com/en-
us/sqlserver/default.aspx
• SQL Tutorial - http://www.sql-tutorial.net/
• Code project:
http://www.codeproject.com/KB/database/Visual_SQL_Joins.aspx
SQL – ONLINE SOURCES
QUESTIONS
Contact Info
• Fox Valley Technical College
• Brian Foote
• foote@fvtc.edu
CONTACT INFO
THANK YOU

More Related Content

What's hot (20)

Intro to SQL for Beginners
Intro to SQL for BeginnersIntro to SQL for Beginners
Intro to SQL for Beginners
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
Oracle: Joins
Oracle: JoinsOracle: Joins
Oracle: Joins
 
Databases
DatabasesDatabases
Databases
 
Sql commands
Sql commandsSql commands
Sql commands
 
Mysql joins
Mysql joinsMysql joins
Mysql joins
 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)
 
SQL
SQLSQL
SQL
 
Database Relationships
Database RelationshipsDatabase Relationships
Database Relationships
 
What is SQL Server?
What is SQL Server?What is SQL Server?
What is SQL Server?
 
Sql commands
Sql commandsSql commands
Sql commands
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)
 
Advanced sql
Advanced sqlAdvanced sql
Advanced sql
 
Sql
SqlSql
Sql
 
Learning sql from w3schools
Learning sql from w3schoolsLearning sql from w3schools
Learning sql from w3schools
 
Access presentacion
Access presentacionAccess presentacion
Access presentacion
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
SQL Joins With Examples | Edureka
SQL Joins With Examples | EdurekaSQL Joins With Examples | Edureka
SQL Joins With Examples | Edureka
 
SQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate TableSQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate Table
 

Similar to SQL Fundamentals

DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxjainendraKUMAR55
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for BeginnersAbdelhay Shafi
 
Prabu's sql quries
Prabu's sql quries Prabu's sql quries
Prabu's sql quries Prabu Cse
 
Sql overview-1232931296681161-1
Sql overview-1232931296681161-1Sql overview-1232931296681161-1
Sql overview-1232931296681161-1sagaroceanic11
 
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQueryPPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQueryAbhishek590097
 
Chapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfChapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfTamiratDejene1
 
SQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptxSQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptxssuser6bf2d1
 
Assignment 4
Assignment 4Assignment 4
Assignment 4SneaK3
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive TechandMate
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSabrinaShanta2
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSaiMiryala1
 
Assg2 b 19121033-converted
Assg2 b 19121033-convertedAssg2 b 19121033-converted
Assg2 b 19121033-convertedSUSHANTPHALKE2
 

Similar to SQL Fundamentals (20)

SQL Query
SQL QuerySQL Query
SQL Query
 
DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptx
 
Babitha2.mysql
Babitha2.mysqlBabitha2.mysql
Babitha2.mysql
 
Babitha2 Mysql
Babitha2 MysqlBabitha2 Mysql
Babitha2 Mysql
 
Query
QueryQuery
Query
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
 
Sql Tutorials
Sql TutorialsSql Tutorials
Sql Tutorials
 
Prabu's sql quries
Prabu's sql quries Prabu's sql quries
Prabu's sql quries
 
Sql slid
Sql slidSql slid
Sql slid
 
Sql overview-1232931296681161-1
Sql overview-1232931296681161-1Sql overview-1232931296681161-1
Sql overview-1232931296681161-1
 
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQueryPPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
 
Chapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfChapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdf
 
Sql
SqlSql
Sql
 
SQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptxSQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptx
 
Assignment 4
Assignment 4Assignment 4
Assignment 4
 
Hira
HiraHira
Hira
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 
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
 
Assg2 b 19121033-converted
Assg2 b 19121033-convertedAssg2 b 19121033-converted
Assg2 b 19121033-converted
 

Recently uploaded

Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durbanmasabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationShrmpro
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 

Recently uploaded (20)

Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 

SQL Fundamentals

  • 1. Structured Query Language Brian Foote Fox Valley Technical College
  • 3. What is the plan for today? • Learn about databases • Learn about SQL Server • Learn about SQL • Whatever else you want to do!
  • 4. • foote@fvtc.edu • 920-735-2568 • Skype : brian.d.foote • Twitter : @footefvtc About Me
  • 5. • UW-Eau Claire • IT Consultant – 18 years • Cap Gemini, Greenbrier & Russell, Fujitsu, Skyline, Rush Lake Consulting • CUNA Mutual Group, Kimberly-Clark, The Real Estate Group, Bemis, Associated Bank, JJ Keller, ThedaCare, Oshkosh Chamber of Oshkosh, Pierce • Husband and father of 3 daughters. • Duck Hunter/Basketball Coach • Teaching – 8th semester Brian Foote
  • 6. SSMS – SQL Server Management Studio – Microsoft’s Query tool to their own database format. HOW?
  • 7. SSMS – SQL SERVER MANAGEMENT STUDIO Start All Programs Microsoft SQL Server SSMS
  • 8. CONNECTING TO SQL SERVER Information you need to connect: Server Database Credentials
  • 9. SSMS – Connecting to SQL Server CONNECTING TO SQL SERVER
  • 10. SQL is short for Structured Query Language and is a widely used database language, providing means of data manipulation (store, retrieve, update, delete) and database creation. SQL Server vs SQL? SQL OVERVIEW
  • 11. • What is SQL and why is it important? • Retrieve data from a single table. • SELECT • SELECT INTO • ORDER BY • WHERE CONDITIONS • AGGREGATE SELECTS SQL OVERVIEW
  • 12. • Retrieve data from a multiple tables. • JOIN (INNER, LEFT, RIGHT, OUTER) • SELECT • SELECT INTO • ORDER BY • WHERE CONDITIONS • AGGREGATE SELECTS SQL OVERVIEW
  • 13. SELECT Statement – Used to retrieve data from the database table. Syntax : SELECT Column1, Column2, Column3, * FROM Table1 Exercise : • Simple column SELECT • Simple * SELECT Example : SELECT * FROM Customers SELECT Name, Address. City, State, ZIP FROM Customers SQL – SELECT – 1 TABLE
  • 14. The SQL ORDER BY clause comes in handy when you want to sort your SQL result sets by some column or columns. Syntax : SELECT * FROM Table1 ORDER BY COLUMN (ORDER BY COLUMN #) Exercise : Simple column SELECT ORDER BY Example : SELECT * FROM Customers ORDER BY City, LastName SQL – SELECT - ORDER
  • 15. The SQL WHERE clause is used to select data conditionally, by adding it to already existing SQL SELECT query. Syntax : SELECT Column1, Column2, Column3, FROM Table1 WHERE <CONDITION> Exercise : Simple column SELECT WHERE Example : SELECT * FROM Customers WHERE State = ‘WI’ SQL – SELECT - WHEREHI
  • 16. The SQL DISTINCT clause is used together with the SQL SELECT keyword, to return a dataset with unique entries for certain database table column. Syntax : SELECT DISTINCT Column1, Column2, Column3, INTO Table2 FROM Table1 Exercise : Simple column SELECT DISTINCT Example : SELECT DISTINCT LastName FROM Customers SQL – SELECT - DISTINCT
  • 17. Condition Must be True or False Relational Operators = Equal <> Not Equal < Less Than <= Less Than or Equal To > Greater than >= Greater than or Equal To The SQL WHERE clause is used to select data conditionally, by adding it to already existing SQL SELECT query. SQL – SELECT - WHERE
  • 18. The SQL AND clause is used when you want to specify more than one condition in your SQL WHERE clause, and at the same time you want all conditions to be true. Syntax : SELECT Column1, Column2, Column3, FROM Table1 WHERE <CONDITION> AND <DIFFERENT CONDITION> Exercise : Simple column SELECT WHERE AND Example : SELECT * FROM Customers WHERE State = ‘WI’ AND CITY = ‘MADISON’ SQL – SELECT - AND
  • 19. The SQL OR statement is used in similar fashion and the major difference compared to the SQL AND is that OR clause will return all rows satisfying any of the conditions listed in the WHERE clause. Syntax : SELECT Column1, Column2, Column3, FROM Table1 WHERE <CONDITION> OR <DIFFERENT CONDITION> Exercise : Simple column SELECT WHERE OR Example : SELECT * FROM Customers WHERE State = ‘WI’ OR State = ‘MN’ SQL – SELECT - OR
  • 20. Other information • Be aware or data type. • String data must in double quotes. • Numeric data does not. • Order of precedence. 1. ~ (Bitwise NOT) 2. * (Multiply), / (Division), % (Modulo) 3. + (Positive), - (Negative), + (Add), (+ Concatenate), - (Subtract) 4. =, >, <, >=, <=, <>, !=, !>, !< (Comparison operators) 5. NOT 6. AND 7. ALL, ANY, BETWEEN, IN, LIKE, OR, SOME 8. = (Assignment) The SQL WHERE clause is used to select data conditionally, by adding it to already existing SQL SELECT query. SQL – SELECT - OOP
  • 21. BETWEEN - TRUE if the operand is within a range. test_expression [ NOT ] BETWEEN begin_expression AND end_expression EXISTS - TRUE if a sub query contains any rows. EXISTS {SELECT STATEMENT} IN - TRUE if the operand is equal to one of a list of expressions. test_expression [ NOT ] IN ( subquery | expression [ ,...n ] ) NOT - Reverses the value of any other Boolean operator. [ NOT ] boolean_expression SQL – SELECT - OTHER
  • 22. LIKE - TRUE if the operand matches a pattern. match_expression [ NOT ] LIKE pattern [ ESCAPE escape_character ] % - Any string of zero or more characters. (WHERE title LIKE '%computer%' ) _ - Any single character. (WHERE au_fname LIKE '_ean') [ ] - Any single character within the specified range ([a-f]) or set ([abcdef]). (WHERE au_lname LIKE '[C-P]arsen' ) [^] - Any single character not within the specified range ([^a-f]) or set ([^abcdef]). (WHERE au_lname LIKE 'de[^l]%' ) SQL – SELECT - LIKE
  • 23. The SQL SELECT INTO statement is used to select data from a SQL database table and to insert it to a different table at the same time. Syntax : SELECT Column1, Column2, Column3, INTO Table2 FROM Table1 Exercise : Simple column SELECT INTO Example : SELECT Name, Address. City, State, ZIP INTO NewCustomers FROM Customers SELECT * FROM Customers SQL SELECT INTO
  • 25. The SQL COUNT clause is used together with the SQL SELECT keyword, to return a dataset with unique entries for certain database table column. Syntax : SELECT COUNT(*) FROM Table1 Exercise : Simple column SELECT COUNT Example : SELECT COUNT(*) FROM Customers SQL – AGGREGATE – COUNT
  • 26. The SQL MAX aggregate function allows us to select the highest (maximum) value for a certain column. Syntax : SELECT MAX(Column1) FROM Table1 Exercise : SELECT MAX Example : SELECT MAX(DOB) AS MaxDOB FROM Customers SQL – AGGREGATE - MAX
  • 27. The SQL MIN aggregate function allows us to select the lowest (minimum) value for a certain column. Syntax : SELECT MIN(Column1) FROM Table1 Exercise : SELECT MIN Example : SELECT MAX(DOB) AS MinDOB FROM Customers SQL – AGGREGATE - MIN
  • 28. The SQL SUM aggregate function allows selecting the total for a numeric column. Syntax : SELECT SUM(Column1) FROM Table1 Exercise : SELECT SUM Example : SELECT SUM(SaleAmount) AS TotalSales FROM Sales SQL – AGGREGATE - SUM
  • 29. The SQL AVG aggregate function allows selecting the total for a numeric column. Syntax : SELECT AVG(Column1) FROM Table1 Exercise : SELECT AVG Example : SELECT AVG(SaleAmount) AS AVGTotalSales FROM Sales SQL – AGGREGATE - AVERAGE
  • 30. The SQL GROUP BY statement is used along with the SQL aggregate functions like SUM to provide means of grouping the result dataset by certain database table column(s). Syntax : SELECT COLUMN2, SUM(Column1) FROM Table1 GROUP BY COLUMN2 Exercise : SELECT GROUP BY Example : SELECT Employee, SUM (Hours) FROM EmployeeHours GROUP BY Employee SQL – AGGREGATE – GROUP BY
  • 31. The SQL HAVING clause is used to restrict conditionally the output of a SQL statement, by a SQL aggregate function used in your SELECT list of columns. Syntax : SELECT COLUMN2, <aggregate function> (Column1) FROM Table1 GROUP BY COLUMN2 HAVING <aggregate function> (Column1) condition Exercise : SELECT HAVING Example : SELECT Employee, SUM (Hours) FROM EmployeeHours GROUP BY Employee HAVING SUM (Hours) > 24 SQL – AGGREGATE HAVING
  • 32. STRUCTURED QUERY LANGUAGE JOINS The SQL JOIN clause is used whenever we have to select data from 2 or more tables. Join Types • INNER - Returns all rows from both tables as long as there is a match between the columns we are matching On. • OUTER – Returns rows from the joined tables if they match or not.
  • 33. The INNER JOIN will select all rows from both tables as long as there is a match between the columns we are matching on. SELECT <select_list> FROM Table_A A INNER JOIN Table_B B ON A.Key = B.Key SQL – INNER JOIN
  • 34. LEFT - The LEFT OUTER JOIN or simply LEFT JOIN (you can omit the OUTER keyword in most databases), selects all the rows from the first table listed after the FROM clause, no matter if they have matches in the second table. SELECT <select_list> FROM Table_A A LEFT JOIN Table_B B ON A.Key = B.Key SQL – LEFT JOIN
  • 35. RIGHT - The RIGHT OUTER JOIN or just RIGHT JOIN behaves exactly as SQL LEFT JOIN, except that it returns all rows from the second table (the right table in our SQL JOIN statement). SELECT <select_list> FROM Table_A A RIGHT JOIN Table_B B ON A.Key = B.Key SQL – RIGHT JOIN
  • 36. FULL OUTER -This query will return all of the records from both tables, joining records from the left table (table A) that match records from the right table (table B). -SELECT <select_list> FROM Table_A A FULL OUTER JOIN Table_B B ON A.Key = B.Key SQL – FULL OUTER
  • 37. Left Excluding JOIN -This query will return all of the records in the left table (table A) that do not match any records in the right table (table B). SELECT <select_list> FROM Table_A A LEFT JOIN Table_B B ON A.Key = B.Key WHERE B.Key IS NULL SQL – LEFT JOIN EXCLUDING
  • 38. Right Excluding JOIN -This query will return all of the records in the right table (table B) that do not match any records in the left table (table A). SELECT <select_list> FROM Table_A A RIGHT JOIN Table_B B ON A.Key = B.Key WHERE A.Key IS NULL
  • 39. Outer Excluding JOIN This query will return all of the records in the left table (table A) and all of the records in the right table (table B) that do not match. SELECT <select_list> FROM Table_A A FULL OUTER JOIN Table_B B ON A.Key = B.Key WHERE A.Key IS NULL OR B.Key IS NULL SQL – OUTER JOIN EXCLUDING
  • 40. Structured Query Language Multiple Tables and Aggregate Operators • COUNT • MAX • MIN • AVG • SUM SQL – AGGREGATE MULTIPLE TABLES
  • 41. Structured Query Language Other Things • Elimination of Nulls (ISNULL) • Use of subquerys when the joins just do not seem right. • Temp Tables (local (#) and global (##) • Export to Excel • Stored Procedures SQL – OTHER THINGS
  • 42. SQL – ALL JOINS
  • 43. Sources • Microsoft Developer Network - http://msdn.microsoft.com/en- us/sqlserver/default.aspx • SQL Tutorial - http://www.sql-tutorial.net/ • Code project: http://www.codeproject.com/KB/database/Visual_SQL_Joins.aspx SQL – ONLINE SOURCES
  • 45. Contact Info • Fox Valley Technical College • Brian Foote • foote@fvtc.edu CONTACT INFO

Editor's Notes

  1. Where at the Cars?
  2. FooteSQLServer (10.4.133.183) CMLogin/ CMLogin
  3. Select * from customers
  4. Select distinct