SlideShare a Scribd company logo
Structured Query Language

Introduction to SQL
What is SQL?
– When a user wants to get some information
from a database file, he can issue a query.
– A query is a user–request to retrieve data or
information with a certain condition.
– SQL is a query language that allows user to
specify the conditions. (instead of algorithms)

Introduction to SQL
Concept of SQL
– The user specifies a certain condition.
– The program will go through all the records
in the database file and select those records
that satisfy the condition.(searching).
– Statistical information of the data.
– The result of the query will then be stored in
form of a table.

Introduction to SQL
Howto involve SQL in FoxPro
– Before using SQL, the tables should be
opened.
– The SQL command can be entered directly
in the Command Window
– To perform exact matching, we should
SET ANSI ON

Basic structure of an
SQL query
General
Structure
SELECT, ALL / DISTINCT, *,
AS, FROM, WHERE
Comparison IN, BETWEEN, LIKE "% _"
Grouping GROUP BY, HAVING,
COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )
Display Order ORDER BY, ASC / DESC
Logical
Operators
AND, OR, NOT
Output INTO TABLE / CURSOR
TO FILE [ADDITIVE], TO PRINTER, TO SCREEN
Union UNION
field type width contents
id
name
dob
sex
characterclass
hcode character
dcode character
remission
mtest
logical
numeric
numeric 4
character 10
date 8
character 1
2
1
3
1
2
student id number
name
date of birth
sex: M / F
class
house code: R, Y, B, G
district code
fee remission
Math test score
The Situation:
Student Particulars

GeneralStructure
SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2] ;
FROM tablename WHEREcondition
SELECT ...... FROM ...... WHERE ......

GeneralStructure
SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2] ;
FROM tablename WHEREcondition
– The query will select rows from the source tablename
and output the result in table form.
– Expressions expr1, expr2 can be :
• (1) a column, or
• (2) an expression of functions and fields.
– And col1, col2 are their corresponding column names in the
output table.

GeneralStructure
– condition can be :
• (1) an inequality, or
• (2) a string comparison
• using logical operators AND, OR, NOT.
SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2] ;
FROM tablename WHEREcondition
– DISTINCT will eliminate duplication in the output while
ALL will keep all duplicated rows.

GeneralStructure
Before using SQL, open the student file:
USE student
eg. 1 List all the student records.
SELECT * FROM student
sex class mtest hcodeid name dob
9801 Peter 06/04/86 M 1A 70 R
9802 Mary 01/10/86 F 1A 92 Y
9803 Johnny 03/16/86 M 1A 91 G
9804 Wendy 07/09/86 F 1B 84 B
dcode remission
SSP .F.
HHM .F.
SSP .T.
YMT .F.
10/17/86 M 1B 88 R9805 Tobe
: : : : : : :
YMT .F.
: :
Result

GeneralStructure
eg. 2 List the names and house code of 1A students.
SELECT name, hcode, class FROM student ;
WHERE class="1A"
Class
1A
1A
1A
1B
1B
:
Class
 1A
 1A
 1A
1B
1B
:


class="1A"

GeneralStructure
name hcode class
Peter R 1A
Mary Y 1A
Johnny G 1A
Luke G 1A
Bobby B 1A
Aaron R 1A
: : :
Result
eg. 2 List the names and house code of 1A students.

GeneralStructure
eg. 3 List the residential district of the Red House members.
SELECT DISTINCT dcode FROM student ;
WHERE hcode="R"
dcode
HHM
KWC
MKK
SSP
TST
YMT
Result

GeneralStructure
eg. 5 List the names, id of 1A students with no fee remission.
SELECT name, id, class FROM student ;
WHERE class="1A" AND NOT remission
name id class
Peter 9801 1A
Mary 9802 1A
Luke 9810 1A
Bobby 9811 1A
Aaron 9812 1A
Ron 9813 1A
Gigi 9824 1A
: : :
Result
Comparison
expr IN ( value1, value2, value3)
expr BETWEEN value1 AND value2
expr LIKE "%_"

Comparison
eg. 6 List the students who were born on Wednesday or Saturdays.
SELECT name, class, CDOW(dob) AS bdate ;
FROM student ;
WHERE DOW(dob) IN (4,7)
name class bdate
Peter 1A Wednesday
Wendy 1B Wednesday
Kevin 1C Saturday
Luke 1A Wednesday
Aaron 1A Saturday
: : :
Result

Comparison
eg. 7 List the students who were not born in January, March, June,
September.
SELECT name, class, dob FROM student ;
WHERE MONTH(dob) NOT IN (1,3,6,9)
name class dob
Wendy 1B 07/09/86
Tobe 1B 10/17/86
Eric 1C 05/05/87
Patty 1C 08/13/87
Kevin 1C 11/21/87
Bobby 1A 02/16/86
Aaron 1A 08/02/86
: : :
Result

Comparison
eg. 8 List the 1A students whose Math test score is between 80 and 90
(incl.)
SELECT name, mtest FROM student ;
WHERE class="1A" AND ;
mtest BETWEEN 80 AND 90
name mtest
Luke 86
Aaron 83
Gigi 84
Result

Comparison
eg. 9 List the students whose names start with "T".
SELECT name, class FROM student ;
WHERE name LIKE "T%"
name class
Tobe 1B
Teddy 1B
Tim 2A
Result

Comparison
eg. 10 List the Red house members whose names contain "a" as the 2nd
letter.
SELECT name, class, hcode FROM student ;
WHERE name LIKE "_a%" AND hcode="R"
Result
name class hcode
Aaron 1A R
Janet 1B R
Paula 2A R

Grouping
SELECT ...... FROM ...... WHERE condition ;
GROUP BY groupexpr [HAVING requirement]
Group functions:
COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )
– groupexpr specifies the related rows to be grouped
as one entry. Usually it is a column.
condition specifies condition ofthe
rows before the rows are group.
– WHERE
individual
HAVING requirement specifies the condition
involving the whole group.

Grouping
eg. 11 List the number of students of each class.
 COUNT( )
Group By Class
COUNT( )
COUNT( )
1A
1B
1C
Student
class
1A
1A
1A
1B
1B
1B
1B
1B
1B
1C
1C
1C

Grouping
class cnt
1A 10
1B 9
1C 9
2A 8
2B 8
2C 6
eg. 11 List the number of students of each class.
SELECT class, COUNT(*) FROM student ;
GROUP BY class
Result

Grouping
eg. 12 List the average Math test score of each class.
AVG( )
AVG( )
AVG( )
1A
1B
1C
Student
Group By Class
class
1A
1A
1A
1B
1B
1B
1B
1B
1B
1C
1C
1C

Grouping
eg. 12 List the average Math test score of each class.
SELECT class, AVG(mtest) FROM student ;
GROUP BY class
class avg_mtest
1A 85.90
1B 70.33
1C 37.89
2A 89.38
2B 53.13
2C 32.67
Result

Grouping
eg. 13 List the number of girls of each district.
SELECT dcode, COUNT(*) FROM student ;
WHERE sex="F" GROUP BY dcode
dcode cnt
HHM 6
KWC 1
MKK 1
SSP 5
TST 4
YMT 8
Result

Grouping
eg. 14 List the max. and min. test score of Form 1 students of each
92 36 HHM
91 19 MKK
91 31 SSP
92 36 TST
75 75 TSW
88 38 YMT
district.
SELECT MAX(mtest), MIN(mtest), dcode ;
FROM student ;
WHERE class LIKE "1_" GROUP BY dcode
max_mtest min_mtest dcode
Result

Grouping
eg. 15 List the average Math test score of the boys in each class. The list
avg_mtest class
86.00 1A
77.75 1B
35.60 1C
86.50 2A
56.50 2B
should not contain class with less than 3 boys.
SELECT AVG(mtest), class FROM student ;
WHERE sex="M" GROUP BY class ;
HAVING COUNT(*) >= 3
Result

Display Order
SELECT ...... FROM ...... WHERE ......
GROUP BY ..... ;
ORDER BY colname ASC / DESC

Display Order
SELECT name, id FROM student ;
WHERE sex="M" AND class="1A" ORDER BY name
eg. 16 List the boys of class 1A, order by their names.
name id
Peter 9801
Johnny 9803
Luke 9810
Bobby 9811
Aaron 9812
Ron 9813
ORDER BY
dcode
name id
Aaron 9812
Bobby 9811
Johnny 9803
Luke 9810
Peter 9801
Ron 9813
Result

Display Order
eg. 17 List the 2A students by their residential district.
SELECT name, id, class, dcode FROM student ;
WHERE class="2A" ORDER BY dcode
name id class dcode
Jimmy 9712 2A HHM
Tim 9713 2A HHM
Samual 9714 2A SHT
Rosa 9703 2A SSP
Helen 9702 2A TST
Joseph 9715 2A TSW
Paula 9701 2A YMT
Susan 9704 2A YMT
Result

Display Order
eg. 18 List the number of students of each district
(in desc. order).
SELECT COUNT(*) AS cnt, dcode FROM student ;
GROUP BY dcode ORDER BY cnt DESC
cnt docode
11 YMT
10 HHM
10 SSP
9 MKK
5 TST
2 TSW
1 KWC
1 MMK
1 SHT
Result

Display Order
eg. 19 List the boys of each house order by the classes. (2-level
ordering)
SELECT name, class, hcode FROM student ;
WHERE sex="M" ORDER BY hcode, class

Display Order
name hcode class
Bobby B 1A
Teddy B 1B
Joseph B 2A
Zion B 2B
Leslie B 2C
Johnny G 1A
Luke G 1A
Kevin G 1C
George G 1C
: : :
Result
Order
by
class
Blue
House
Green
House
:
:
Order
by
hcode

Output
INTO TABLE tablename the output table is saved as a
database file in the disk.
INTO CURSOR temp the output is stored in the
working memory temporarily.
TO FILE filename [ADDITIVE] output to a text file.
(additive = append)
TO PRINTER send to printer.
TO SCREEN display on screen.

Output
eg. 20 List the students in desc. order of their names and save the result
as a database file name.dbf.
SELECT * FROM student ;
ORDER BY name DESC INTO TABLE name.dbf
id name dob sex class mtest hcode dcode remission
9707 Zion 07/29/85 M 2B 51 B MKK .F.
9709 Yvonne 08/24/85 F 2C 10 R TST .F.
9804 Wendy 07/09/86 F 1B 84 B YMT .F.
9819 Vincent 03/15/85 M 1C 29 Y MKK .F.
9805 Tobe 10/17/86 M 1B 88 R YMT .F.
9713 Tim 06/19/85 M 2A 91 R HHM .T.
9816 Teddy 01/30/86 M 1B 64 B SSP .F.
: : : : : : : : :
Result

Output
eg. 21 Print the Red House members by their classes, sex and name.
SELECT class, name, sex FROM student ;
WHERE hcode="R" ;
ORDER BY class, sex DESC, name TO PRINTER
class name sex
1A Aaron M
1A Peter M
1A Ron M
1B Tobe M
1B Janet F
1B Kitty F
1B Mimi F
: : :
Result

Union, Intersection
and Difference of
Tables
UNION ;
SELECT * FROM chess ;
ORDER BY class, name INTO TABLE party
eg. 22 The two clubs want to hold a joint party. Make a list of all
students. (Union)
SELECT * FROM bridge ;
SELECT ...... FROM ...... WHERE ...... ;
UNION ;
SELECT ...... FROM ...... WHERE ......
Result

Union, Intersection
and Difference of
Tables
SELECT * FROM bridge ;
WHERE id IN ( SELECT id FROM chess ) ;
TO PRINTER
eg. 23 Print a list of students who are members of both clubs.
(Intersection)
SELECT ...... FROM table1 ;
WHERE col IN ( SELECT col FROM table2 )
Result

Union, Intersection
and Difference of
Tables
SELECT * FROM bridge ;
WHERE id NOT IN ( SELECT id FROM chess ) ;
INTO TABLE diff
eg. 24 Make a list of students who are members of the Bridge Club but
not Chess Club. (Difference)
SELECT ...... FROM table1 ;
WHERE col NOT IN ( SELECT col FROM table2 )
Result

Multiple Tables:
• SQL provides a convenient operation to
retrieve information from multiple tables.
• This operation is called join.
• The join operation will combine the tables into
one large table with all possible combinations
(Math: Cartesian Product), and then it will
filter
useful
the rows of this combined table to yield
information.

id numeric
type character
4 student id number
10 type of the music instrument
The Situation:
Music Lesson
Each student should learn a musical instrument.
Two database files: student.dbf & music.dbf
The common field: student id
field type width contents
SELECTA
USE student
SELECT B
USE music
A Natural Join is a join operation that joins two
tables by their common column. This operation
is similar to the setting relation of two tables.
SELECT a.comcol, a.col1, b.col2, expr1, expr2 ;
FROM table1 a, table2 b ;
WHERE a.comcol = b.comcol

Natural Join
Music
id
980
1
type
Student
9801
id name class
9801
Product
id name class type
Same id
Join
eg. 25 Make a list of students and the instruments they
learn. (Natural Join)

Natural Join
cl
a
ss name id type
1
A
Aaron 9812 Piano
1
A
Bobby 9811 Flute
1
A
Gigi 9824 Recorder
1
A
Jil 9820 Piano
1
A
Johnny 9803 Violin
WHERE s.id=m.id ORDER BY class, name
Result
eg. 25 Make a list of students and the instruments they
learn.
(Natural Join)
SELECT s.class, s.name, s.id, m.type ;
FROM student s, music m ;
eg. 26 Find the number of students learning piano in each class.
Natural Join
Three Parts :
(1) Natural Join.
(2) Condition: m.type="Piano"
(3) GROUP BY class

Student
Product
Music
Join Condition
m.type= "Piano"
Group By
class
Natural Join
eg. 26
eg. 26 Find the number of students learning piano in each class.
Natural Join
class cnt
1A 4
1B 2
1C 1
SELECT s.class, COUNT(*) ;
FROM student s, music m ;
WHERE s.id=m.id AND m.type="Piano" ;
GROUP BY class ORDER BY class
Result
An Outer Join is a join operation that includes
rows that have a match, plus rows that do not
have a match in the other table.
Outer Join
eg. 27 List the students who have not yet chosen an instrument. (No
match)
Outer Join
No match
Music
id type
Student
9801
id name class
eg. 27 List the students who have not yet chosen an instrument. (No
 match)
 SELECT class, name, id FROM student ;
WHERE id NOT IN ( SELECT id FROM
music ) ;
ORDER BY class, name
Outer Join
Result
class name id
1A Mandy 9821
1B Kenny 9814
1B Tobe 9805
1C Edmond 9818
1C George 9817
: : :
eg. 28 Make a checking list of students and the instruments they learn. The list
should also contain the students without an instrument.
(Outer Join)
Outer Join

Outer Join
Natural Join
Outer Join
No Match
eg. 28

Outer Join
eg. 2S8ELECTs.class, s.name, s.id, m.type ;
FROM student s, music m ;
WHERE s.id=m.id ;
UNION ;
SELECT class, name, id, "" ;
FROM student ;
WHERE id NOT IN ( SELECT id FROM music ) ;
ORDER BY 1, 2

emptyclass name id
1A Mandy 9821
1B Kenny 9814
1B Tobe 9805
1C Edmond 9818
1C George 9817
: : :
No Match
Outer Join
class name id type1A Aaron 9812 Piano
1A Bobby 9811 Flute
1A Gigi 9824 Recorder
1A Jil 9820 Piano
1A Johnny 9803 Violin
1A Luke 9810 Piano
1A Mary 9802 Flute
: : : :
Natural Join
class name id type
1A Aaron 9812 Piano
1A Bobby 9811 Flute
1A Gigi 9824 Recorder
1A Jil 9820 Piano
1A Johnny 9803 Violin
1A Luke 9810 Piano
1A Mandy 9821
1A Mary 9802 Flute
1A Peter 9801 Piano
1A Ron 9813 Guitar
1B Eddy 9815 Piano
1B Janet 9822 Guitar
1B Kenny 9814
1B Kitty 9806 Recorder
: : : :
Outer Join
Ad

More Related Content

What's hot (20)

SQL Overview
SQL OverviewSQL Overview
SQL Overview
Stewart Rogers
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
Nishant Munjal
 
MySQL and its basic commands
MySQL and its basic commandsMySQL and its basic commands
MySQL and its basic commands
Bwsrang Basumatary
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
VARSHAKUMARI49
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
Md.Mojibul Hoque
 
SQL
SQLSQL
SQL
Reimuel Bisnar
 
Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)
Punjab University
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
webhostingguy
 
Sql commands
Sql commandsSql commands
Sql commands
Balakumaran Arunachalam
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)
Ishucs
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
Dhananjay Goel
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
BG Java EE Course
 
Introduction to Mysql
Introduction to MysqlIntroduction to Mysql
Introduction to Mysql
Tushar Chauhan
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
Introduction to Oracle Database
Introduction to Oracle DatabaseIntroduction to Oracle Database
Introduction to Oracle Database
puja_dhar
 
Oracle
OracleOracle
Oracle
JIGAR MAKHIJA
 
DML Commands
DML CommandsDML Commands
DML Commands
Randy Riness @ South Puget Sound Community College
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
Hammad Rasheed
 
Introduction of sql server indexing
Introduction of sql server indexingIntroduction of sql server indexing
Introduction of sql server indexing
Mahabubur Rahaman
 
Chapter 4 Structured Query Language
Chapter 4 Structured Query LanguageChapter 4 Structured Query Language
Chapter 4 Structured Query Language
Eddyzulham Mahluzydde
 

Similar to Sql queries presentation (20)

this is a SQL ppt you can find the basic sql queries
this is a SQL ppt you can find the basic sql queriesthis is a SQL ppt you can find the basic sql queries
this is a SQL ppt you can find the basic sql queries
deepak266689
 
sqlIntroduction to StructuredQueryLanguage.ppt
sqlIntroduction to StructuredQueryLanguage.pptsqlIntroduction to StructuredQueryLanguage.ppt
sqlIntroduction to StructuredQueryLanguage.ppt
yp02
 
Sqlppt 120914120535-phpapp01
Sqlppt 120914120535-phpapp01Sqlppt 120914120535-phpapp01
Sqlppt 120914120535-phpapp01
Ankit Dubey
 
Sql
SqlSql
Sql
chandni agarwal
 
RDBMS Algebra
RDBMS AlgebraRDBMS Algebra
RDBMS Algebra
Sarmad Ali
 
Chapter-4.ppt
Chapter-4.pptChapter-4.ppt
Chapter-4.ppt
CindyCuesta
 
O Level Paper 2 database All topics of chapter
O Level Paper 2 database All topics of chapterO Level Paper 2 database All topics of chapter
O Level Paper 2 database All topics of chapter
NoumanShamim1
 
Sql Basics | Edureka
Sql Basics | EdurekaSql Basics | Edureka
Sql Basics | Edureka
Edureka!
 
dbms.pdf
dbms.pdfdbms.pdf
dbms.pdf
walter brand
 
My Sql
My Sql My Sql
My Sql
Sai Sathvick Chirakala
 
Normalization
NormalizationNormalization
Normalization
Tobe Tennyson
 
SQL
SQLSQL
SQL
Shyam Khant
 
Graphical presentation of data
Graphical presentation of dataGraphical presentation of data
Graphical presentation of data
prince irfan
 
Fundamentals of Database management system Lab Manual.pptx
Fundamentals of Database management system Lab Manual.pptxFundamentals of Database management system Lab Manual.pptx
Fundamentals of Database management system Lab Manual.pptx
Getnet Tigabie Askale -(GM)
 
Sql basics v2
Sql basics v2Sql basics v2
Sql basics v2
Yousuf Akhtar Sultan
 
Lab4 join - all types listed
Lab4   join - all types listedLab4   join - all types listed
Lab4 join - all types listed
Balqees Al.Mubarak
 
MS Excel Detailed into.pptx
MS Excel Detailed into.pptxMS Excel Detailed into.pptx
MS Excel Detailed into.pptx
AssadLeo1
 
2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd
2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd
2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd
DevKartikSharma1
 
SQL- MOST IMPORTANT CONCEPTS
SQL- MOST IMPORTANT CONCEPTSSQL- MOST IMPORTANT CONCEPTS
SQL- MOST IMPORTANT CONCEPTS
Gagandeep Nanda
 
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
sahilurrahemankhan
 
this is a SQL ppt you can find the basic sql queries
this is a SQL ppt you can find the basic sql queriesthis is a SQL ppt you can find the basic sql queries
this is a SQL ppt you can find the basic sql queries
deepak266689
 
sqlIntroduction to StructuredQueryLanguage.ppt
sqlIntroduction to StructuredQueryLanguage.pptsqlIntroduction to StructuredQueryLanguage.ppt
sqlIntroduction to StructuredQueryLanguage.ppt
yp02
 
Sqlppt 120914120535-phpapp01
Sqlppt 120914120535-phpapp01Sqlppt 120914120535-phpapp01
Sqlppt 120914120535-phpapp01
Ankit Dubey
 
O Level Paper 2 database All topics of chapter
O Level Paper 2 database All topics of chapterO Level Paper 2 database All topics of chapter
O Level Paper 2 database All topics of chapter
NoumanShamim1
 
Sql Basics | Edureka
Sql Basics | EdurekaSql Basics | Edureka
Sql Basics | Edureka
Edureka!
 
Graphical presentation of data
Graphical presentation of dataGraphical presentation of data
Graphical presentation of data
prince irfan
 
Fundamentals of Database management system Lab Manual.pptx
Fundamentals of Database management system Lab Manual.pptxFundamentals of Database management system Lab Manual.pptx
Fundamentals of Database management system Lab Manual.pptx
Getnet Tigabie Askale -(GM)
 
MS Excel Detailed into.pptx
MS Excel Detailed into.pptxMS Excel Detailed into.pptx
MS Excel Detailed into.pptx
AssadLeo1
 
2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd
2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd
2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd
DevKartikSharma1
 
SQL- MOST IMPORTANT CONCEPTS
SQL- MOST IMPORTANT CONCEPTSSQL- MOST IMPORTANT CONCEPTS
SQL- MOST IMPORTANT CONCEPTS
Gagandeep Nanda
 
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
sahilurrahemankhan
 
Ad

Recently uploaded (20)

03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
Computer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issuesComputer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issues
Abhijit Bodhe
 
Tax evasion, Tax planning & Tax avoidance.pptx
Tax evasion, Tax  planning &  Tax avoidance.pptxTax evasion, Tax  planning &  Tax avoidance.pptx
Tax evasion, Tax planning & Tax avoidance.pptx
manishbaidya2017
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
Kumushini_Thennakoon_CAPWIC_slides_.pptx
Kumushini_Thennakoon_CAPWIC_slides_.pptxKumushini_Thennakoon_CAPWIC_slides_.pptx
Kumushini_Thennakoon_CAPWIC_slides_.pptx
kumushiniodu
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18
Celine George
 
How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
Lecture 2 CLASSIFICATION OF PHYLUM ARTHROPODA UPTO CLASSES & POSITION OF_1.pptx
Lecture 2 CLASSIFICATION OF PHYLUM ARTHROPODA UPTO CLASSES & POSITION OF_1.pptxLecture 2 CLASSIFICATION OF PHYLUM ARTHROPODA UPTO CLASSES & POSITION OF_1.pptx
Lecture 2 CLASSIFICATION OF PHYLUM ARTHROPODA UPTO CLASSES & POSITION OF_1.pptx
Arshad Shaikh
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 
Lecture 1 Introduction history and institutes of entomology_1.pptx
Lecture 1 Introduction history and institutes of entomology_1.pptxLecture 1 Introduction history and institutes of entomology_1.pptx
Lecture 1 Introduction history and institutes of entomology_1.pptx
Arshad Shaikh
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
Computer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issuesComputer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issues
Abhijit Bodhe
 
Tax evasion, Tax planning & Tax avoidance.pptx
Tax evasion, Tax  planning &  Tax avoidance.pptxTax evasion, Tax  planning &  Tax avoidance.pptx
Tax evasion, Tax planning & Tax avoidance.pptx
manishbaidya2017
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
Kumushini_Thennakoon_CAPWIC_slides_.pptx
Kumushini_Thennakoon_CAPWIC_slides_.pptxKumushini_Thennakoon_CAPWIC_slides_.pptx
Kumushini_Thennakoon_CAPWIC_slides_.pptx
kumushiniodu
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18
Celine George
 
How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
Lecture 2 CLASSIFICATION OF PHYLUM ARTHROPODA UPTO CLASSES & POSITION OF_1.pptx
Lecture 2 CLASSIFICATION OF PHYLUM ARTHROPODA UPTO CLASSES & POSITION OF_1.pptxLecture 2 CLASSIFICATION OF PHYLUM ARTHROPODA UPTO CLASSES & POSITION OF_1.pptx
Lecture 2 CLASSIFICATION OF PHYLUM ARTHROPODA UPTO CLASSES & POSITION OF_1.pptx
Arshad Shaikh
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 
Lecture 1 Introduction history and institutes of entomology_1.pptx
Lecture 1 Introduction history and institutes of entomology_1.pptxLecture 1 Introduction history and institutes of entomology_1.pptx
Lecture 1 Introduction history and institutes of entomology_1.pptx
Arshad Shaikh
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
Ad

Sql queries presentation

  • 2.  Introduction to SQL What is SQL? – When a user wants to get some information from a database file, he can issue a query. – A query is a user–request to retrieve data or information with a certain condition. – SQL is a query language that allows user to specify the conditions. (instead of algorithms)
  • 3.  Introduction to SQL Concept of SQL – The user specifies a certain condition. – The program will go through all the records in the database file and select those records that satisfy the condition.(searching). – Statistical information of the data. – The result of the query will then be stored in form of a table.
  • 4.  Introduction to SQL Howto involve SQL in FoxPro – Before using SQL, the tables should be opened. – The SQL command can be entered directly in the Command Window – To perform exact matching, we should SET ANSI ON
  • 5.  Basic structure of an SQL query General Structure SELECT, ALL / DISTINCT, *, AS, FROM, WHERE Comparison IN, BETWEEN, LIKE "% _" Grouping GROUP BY, HAVING, COUNT( ), SUM( ), AVG( ), MAX( ), MIN( ) Display Order ORDER BY, ASC / DESC Logical Operators AND, OR, NOT Output INTO TABLE / CURSOR TO FILE [ADDITIVE], TO PRINTER, TO SCREEN Union UNION
  • 6. field type width contents id name dob sex characterclass hcode character dcode character remission mtest logical numeric numeric 4 character 10 date 8 character 1 2 1 3 1 2 student id number name date of birth sex: M / F class house code: R, Y, B, G district code fee remission Math test score The Situation: Student Particulars
  • 7.  GeneralStructure SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2] ; FROM tablename WHEREcondition SELECT ...... FROM ...... WHERE ......
  • 8.  GeneralStructure SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2] ; FROM tablename WHEREcondition – The query will select rows from the source tablename and output the result in table form. – Expressions expr1, expr2 can be : • (1) a column, or • (2) an expression of functions and fields. – And col1, col2 are their corresponding column names in the output table.
  • 9.  GeneralStructure – condition can be : • (1) an inequality, or • (2) a string comparison • using logical operators AND, OR, NOT. SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2] ; FROM tablename WHEREcondition – DISTINCT will eliminate duplication in the output while ALL will keep all duplicated rows.
  • 10.  GeneralStructure Before using SQL, open the student file: USE student eg. 1 List all the student records. SELECT * FROM student sex class mtest hcodeid name dob 9801 Peter 06/04/86 M 1A 70 R 9802 Mary 01/10/86 F 1A 92 Y 9803 Johnny 03/16/86 M 1A 91 G 9804 Wendy 07/09/86 F 1B 84 B dcode remission SSP .F. HHM .F. SSP .T. YMT .F. 10/17/86 M 1B 88 R9805 Tobe : : : : : : : YMT .F. : : Result
  • 11.  GeneralStructure eg. 2 List the names and house code of 1A students. SELECT name, hcode, class FROM student ; WHERE class="1A" Class 1A 1A 1A 1B 1B : Class  1A  1A  1A 1B 1B :   class="1A"
  • 12.  GeneralStructure name hcode class Peter R 1A Mary Y 1A Johnny G 1A Luke G 1A Bobby B 1A Aaron R 1A : : : Result eg. 2 List the names and house code of 1A students.
  • 13.  GeneralStructure eg. 3 List the residential district of the Red House members. SELECT DISTINCT dcode FROM student ; WHERE hcode="R" dcode HHM KWC MKK SSP TST YMT Result
  • 14.  GeneralStructure eg. 5 List the names, id of 1A students with no fee remission. SELECT name, id, class FROM student ; WHERE class="1A" AND NOT remission name id class Peter 9801 1A Mary 9802 1A Luke 9810 1A Bobby 9811 1A Aaron 9812 1A Ron 9813 1A Gigi 9824 1A : : : Result
  • 15. Comparison expr IN ( value1, value2, value3) expr BETWEEN value1 AND value2 expr LIKE "%_"
  • 16.  Comparison eg. 6 List the students who were born on Wednesday or Saturdays. SELECT name, class, CDOW(dob) AS bdate ; FROM student ; WHERE DOW(dob) IN (4,7) name class bdate Peter 1A Wednesday Wendy 1B Wednesday Kevin 1C Saturday Luke 1A Wednesday Aaron 1A Saturday : : : Result
  • 17.  Comparison eg. 7 List the students who were not born in January, March, June, September. SELECT name, class, dob FROM student ; WHERE MONTH(dob) NOT IN (1,3,6,9) name class dob Wendy 1B 07/09/86 Tobe 1B 10/17/86 Eric 1C 05/05/87 Patty 1C 08/13/87 Kevin 1C 11/21/87 Bobby 1A 02/16/86 Aaron 1A 08/02/86 : : : Result
  • 18.  Comparison eg. 8 List the 1A students whose Math test score is between 80 and 90 (incl.) SELECT name, mtest FROM student ; WHERE class="1A" AND ; mtest BETWEEN 80 AND 90 name mtest Luke 86 Aaron 83 Gigi 84 Result
  • 19.  Comparison eg. 9 List the students whose names start with "T". SELECT name, class FROM student ; WHERE name LIKE "T%" name class Tobe 1B Teddy 1B Tim 2A Result
  • 20.  Comparison eg. 10 List the Red house members whose names contain "a" as the 2nd letter. SELECT name, class, hcode FROM student ; WHERE name LIKE "_a%" AND hcode="R" Result name class hcode Aaron 1A R Janet 1B R Paula 2A R
  • 21.  Grouping SELECT ...... FROM ...... WHERE condition ; GROUP BY groupexpr [HAVING requirement] Group functions: COUNT( ), SUM( ), AVG( ), MAX( ), MIN( ) – groupexpr specifies the related rows to be grouped as one entry. Usually it is a column. condition specifies condition ofthe rows before the rows are group. – WHERE individual HAVING requirement specifies the condition involving the whole group.
  • 22.  Grouping eg. 11 List the number of students of each class.
  • 23.  COUNT( ) Group By Class COUNT( ) COUNT( ) 1A 1B 1C Student class 1A 1A 1A 1B 1B 1B 1B 1B 1B 1C 1C 1C
  • 24.  Grouping class cnt 1A 10 1B 9 1C 9 2A 8 2B 8 2C 6 eg. 11 List the number of students of each class. SELECT class, COUNT(*) FROM student ; GROUP BY class Result
  • 25.  Grouping eg. 12 List the average Math test score of each class.
  • 26. AVG( ) AVG( ) AVG( ) 1A 1B 1C Student Group By Class class 1A 1A 1A 1B 1B 1B 1B 1B 1B 1C 1C 1C
  • 27.  Grouping eg. 12 List the average Math test score of each class. SELECT class, AVG(mtest) FROM student ; GROUP BY class class avg_mtest 1A 85.90 1B 70.33 1C 37.89 2A 89.38 2B 53.13 2C 32.67 Result
  • 28.  Grouping eg. 13 List the number of girls of each district. SELECT dcode, COUNT(*) FROM student ; WHERE sex="F" GROUP BY dcode dcode cnt HHM 6 KWC 1 MKK 1 SSP 5 TST 4 YMT 8 Result
  • 29.  Grouping eg. 14 List the max. and min. test score of Form 1 students of each 92 36 HHM 91 19 MKK 91 31 SSP 92 36 TST 75 75 TSW 88 38 YMT district. SELECT MAX(mtest), MIN(mtest), dcode ; FROM student ; WHERE class LIKE "1_" GROUP BY dcode max_mtest min_mtest dcode Result
  • 30.  Grouping eg. 15 List the average Math test score of the boys in each class. The list avg_mtest class 86.00 1A 77.75 1B 35.60 1C 86.50 2A 56.50 2B should not contain class with less than 3 boys. SELECT AVG(mtest), class FROM student ; WHERE sex="M" GROUP BY class ; HAVING COUNT(*) >= 3 Result
  • 31.  Display Order SELECT ...... FROM ...... WHERE ...... GROUP BY ..... ; ORDER BY colname ASC / DESC
  • 32.  Display Order SELECT name, id FROM student ; WHERE sex="M" AND class="1A" ORDER BY name eg. 16 List the boys of class 1A, order by their names. name id Peter 9801 Johnny 9803 Luke 9810 Bobby 9811 Aaron 9812 Ron 9813 ORDER BY dcode name id Aaron 9812 Bobby 9811 Johnny 9803 Luke 9810 Peter 9801 Ron 9813 Result
  • 33.  Display Order eg. 17 List the 2A students by their residential district. SELECT name, id, class, dcode FROM student ; WHERE class="2A" ORDER BY dcode name id class dcode Jimmy 9712 2A HHM Tim 9713 2A HHM Samual 9714 2A SHT Rosa 9703 2A SSP Helen 9702 2A TST Joseph 9715 2A TSW Paula 9701 2A YMT Susan 9704 2A YMT Result
  • 34.  Display Order eg. 18 List the number of students of each district (in desc. order). SELECT COUNT(*) AS cnt, dcode FROM student ; GROUP BY dcode ORDER BY cnt DESC cnt docode 11 YMT 10 HHM 10 SSP 9 MKK 5 TST 2 TSW 1 KWC 1 MMK 1 SHT Result
  • 35.  Display Order eg. 19 List the boys of each house order by the classes. (2-level ordering) SELECT name, class, hcode FROM student ; WHERE sex="M" ORDER BY hcode, class
  • 36.  Display Order name hcode class Bobby B 1A Teddy B 1B Joseph B 2A Zion B 2B Leslie B 2C Johnny G 1A Luke G 1A Kevin G 1C George G 1C : : : Result Order by class Blue House Green House : : Order by hcode
  • 37.  Output INTO TABLE tablename the output table is saved as a database file in the disk. INTO CURSOR temp the output is stored in the working memory temporarily. TO FILE filename [ADDITIVE] output to a text file. (additive = append) TO PRINTER send to printer. TO SCREEN display on screen.
  • 38.  Output eg. 20 List the students in desc. order of their names and save the result as a database file name.dbf. SELECT * FROM student ; ORDER BY name DESC INTO TABLE name.dbf id name dob sex class mtest hcode dcode remission 9707 Zion 07/29/85 M 2B 51 B MKK .F. 9709 Yvonne 08/24/85 F 2C 10 R TST .F. 9804 Wendy 07/09/86 F 1B 84 B YMT .F. 9819 Vincent 03/15/85 M 1C 29 Y MKK .F. 9805 Tobe 10/17/86 M 1B 88 R YMT .F. 9713 Tim 06/19/85 M 2A 91 R HHM .T. 9816 Teddy 01/30/86 M 1B 64 B SSP .F. : : : : : : : : : Result
  • 39.  Output eg. 21 Print the Red House members by their classes, sex and name. SELECT class, name, sex FROM student ; WHERE hcode="R" ; ORDER BY class, sex DESC, name TO PRINTER class name sex 1A Aaron M 1A Peter M 1A Ron M 1B Tobe M 1B Janet F 1B Kitty F 1B Mimi F : : : Result
  • 40.  Union, Intersection and Difference of Tables UNION ; SELECT * FROM chess ; ORDER BY class, name INTO TABLE party eg. 22 The two clubs want to hold a joint party. Make a list of all students. (Union) SELECT * FROM bridge ; SELECT ...... FROM ...... WHERE ...... ; UNION ; SELECT ...... FROM ...... WHERE ...... Result
  • 41.  Union, Intersection and Difference of Tables SELECT * FROM bridge ; WHERE id IN ( SELECT id FROM chess ) ; TO PRINTER eg. 23 Print a list of students who are members of both clubs. (Intersection) SELECT ...... FROM table1 ; WHERE col IN ( SELECT col FROM table2 ) Result
  • 42.  Union, Intersection and Difference of Tables SELECT * FROM bridge ; WHERE id NOT IN ( SELECT id FROM chess ) ; INTO TABLE diff eg. 24 Make a list of students who are members of the Bridge Club but not Chess Club. (Difference) SELECT ...... FROM table1 ; WHERE col NOT IN ( SELECT col FROM table2 ) Result
  • 43.  Multiple Tables: • SQL provides a convenient operation to retrieve information from multiple tables. • This operation is called join. • The join operation will combine the tables into one large table with all possible combinations (Math: Cartesian Product), and then it will filter useful the rows of this combined table to yield information.
  • 44.  id numeric type character 4 student id number 10 type of the music instrument The Situation: Music Lesson Each student should learn a musical instrument. Two database files: student.dbf & music.dbf The common field: student id field type width contents SELECTA USE student SELECT B USE music
  • 45. A Natural Join is a join operation that joins two tables by their common column. This operation is similar to the setting relation of two tables. SELECT a.comcol, a.col1, b.col2, expr1, expr2 ; FROM table1 a, table2 b ; WHERE a.comcol = b.comcol
  • 46.  Natural Join Music id 980 1 type Student 9801 id name class 9801 Product id name class type Same id Join eg. 25 Make a list of students and the instruments they learn. (Natural Join)
  • 47.  Natural Join cl a ss name id type 1 A Aaron 9812 Piano 1 A Bobby 9811 Flute 1 A Gigi 9824 Recorder 1 A Jil 9820 Piano 1 A Johnny 9803 Violin WHERE s.id=m.id ORDER BY class, name Result eg. 25 Make a list of students and the instruments they learn. (Natural Join) SELECT s.class, s.name, s.id, m.type ; FROM student s, music m ;
  • 48. eg. 26 Find the number of students learning piano in each class. Natural Join Three Parts : (1) Natural Join. (2) Condition: m.type="Piano" (3) GROUP BY class
  • 50. eg. 26 Find the number of students learning piano in each class. Natural Join class cnt 1A 4 1B 2 1C 1 SELECT s.class, COUNT(*) ; FROM student s, music m ; WHERE s.id=m.id AND m.type="Piano" ; GROUP BY class ORDER BY class Result
  • 51. An Outer Join is a join operation that includes rows that have a match, plus rows that do not have a match in the other table. Outer Join
  • 52. eg. 27 List the students who have not yet chosen an instrument. (No match) Outer Join No match Music id type Student 9801 id name class
  • 53. eg. 27 List the students who have not yet chosen an instrument. (No  match)  SELECT class, name, id FROM student ; WHERE id NOT IN ( SELECT id FROM music ) ; ORDER BY class, name Outer Join Result class name id 1A Mandy 9821 1B Kenny 9814 1B Tobe 9805 1C Edmond 9818 1C George 9817 : : :
  • 54. eg. 28 Make a checking list of students and the instruments they learn. The list should also contain the students without an instrument. (Outer Join) Outer Join
  • 55.  Outer Join Natural Join Outer Join No Match eg. 28
  • 56.  Outer Join eg. 2S8ELECTs.class, s.name, s.id, m.type ; FROM student s, music m ; WHERE s.id=m.id ; UNION ; SELECT class, name, id, "" ; FROM student ; WHERE id NOT IN ( SELECT id FROM music ) ; ORDER BY 1, 2
  • 57.  emptyclass name id 1A Mandy 9821 1B Kenny 9814 1B Tobe 9805 1C Edmond 9818 1C George 9817 : : : No Match Outer Join class name id type1A Aaron 9812 Piano 1A Bobby 9811 Flute 1A Gigi 9824 Recorder 1A Jil 9820 Piano 1A Johnny 9803 Violin 1A Luke 9810 Piano 1A Mary 9802 Flute : : : : Natural Join class name id type 1A Aaron 9812 Piano 1A Bobby 9811 Flute 1A Gigi 9824 Recorder 1A Jil 9820 Piano 1A Johnny 9803 Violin 1A Luke 9810 Piano 1A Mandy 9821 1A Mary 9802 Flute 1A Peter 9801 Piano 1A Ron 9813 Guitar 1B Eddy 9815 Piano 1B Janet 9822 Guitar 1B Kenny 9814 1B Kitty 9806 Recorder : : : : Outer Join
  翻译: