Structured Query Language: Abhishek Bhardwaj Asst. Prof IT
Structured Query Language: Abhishek Bhardwaj Asst. Prof IT
Syntax
SELECT column_name(s)
FROM table_name
výsledok
LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari
Orders
Company
Sega
Company OrderNumber W3Schools
Sega 3412 Trio
W3Schools 2312
Trio 4678
W3Schools 6798
Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
The following SQL statement will return persons with first names
that end with an 'a':
SELECT * FROM Persons
WHERE FirstName LIKE '%a'
Abhishek Bhardwaj Asst. Prof IT
Using LIKE 2
The following SQL statement will return persons with first names
that contain the pattern 'la':
SELECT * FROM Persons
WHERE FirstName LIKE '%la%'
You can also specify the columns for which you want to insert
data:
INSERT INTO table_name (column1, column2,...)
VALUES (value1, value2,....)
Abhishek Bhardwaj Asst. Prof IT
Insert a New Row
LastName FirstName Address City
UPDATE table_name
SET column_name = new_value
WHERE column_name = some_value
We want to change the address and add the name of the city:
UPDATE Person
SET Address = 'Stien 12', City = 'Stavanger'
WHERE LastName = 'Rasmussen'
Group functions:
COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )
1A 1A
1A
COUNT( )
1B
1B
1B
1B 1B
COUNT( )
1B
1B
1C
1C 1C
1C
COUNT( )
Student
Abhishek Bhardwaj Asst. Prof IT
III Grouping
eg. 11 List the number of students of each class.
SELECT class, COUNT(*) FROM student
GROUP BY class
class cnt
Result 1A 10
1B 9
1C 9
2A 8
2B 8
2C 6
Abhishek Bhardwaj Asst. Prof IT
III Grouping
eg. 12 List the average Math test score of each class.
1A 1A
1A
AVG( )
1B
1B
1B
1B
AVG( )
1B
1B
1B
1C
1C 1C AVG( )
1C
Student
Abhishek Bhardwaj Asst. Prof IT
III Grouping
eg. 12 List the average Math test score of each class.
field1 field2
A 1
field1 field2
A 2
A 1
A 3
B 2
B 1
3
B 2
B 3
SELECT A
USE student
SELECT B
USE music
Abhishek Bhardwaj Asst. Prof IT
4 Natural Join
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.
Same id 9801
9801
Join
Student Music
9801
Abhishek Bhardwaj Asst. Prof IT
Product
4
eg. 25
Natural Join
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 ;
WHERE s.id=m.id ORDER BY class, name
class name id type
Result 1A Aaron 9812 Piano
1A Bobby 9811 Flute
1A Gigi 9824 Recorder
1A Jill 9820 Piano
1A Johnny 9803 Violin
1A Luke 9810 Piano
1A Mary 9802 Flute
: : : :
Abhishek Bhardwaj Asst. Prof IT
4 Outer Join
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.
9801
No match
Student Music
Natural Join
Outer Join
No Match
Abhishek Bhardwaj Asst. Prof IT
4
class name id type
Outer Join
class name id type
1A Aaron 9812 Piano
1A Bobby 9811 Flute 1A Aaron 9812 Piano
1A Gigi 9824 Recorder 1A Bobby 9811 Flute
1A Jill 9820 Piano 1A Gigi 9824 Recorder
1A Johnny 9803 Violin 1A Jill 9820 Piano
1A Luke 9810 Piano 1A Johnny 9803 Violin
1A Mary 9802 Flute 1A Luke 9810 Piano
: : : : 1A Mandy 9821
Natural Join 1A Mary 9802 Flute
1A Peter 9801 Piano
class name id
1A Mandy 9821
1A Ron 9813 Guitar empty
1B Eddy 9815 Piano
1B Kenny 9814
1B Tobe 9805 1B Janet 9822 Guitar
1C Edmond 9818 1B Kenny 9814
1C George 9817 1B Kitty 9806 Recorder
: : : : : : :
No Match
Abhishek Bhardwaj Asst. Prof IT Outer Join
Joined Relations
Join operations take two relations and return as a result another
relation.
These additional operations are typically used as subquery
expressions in the from clause
Join condition – defines which tuples in the two relations match,
and what attributes are present in the result of the join.
Join type – defines how tuples in each relation that do not match
any tuple in the other relation (based on the join condition) are
treated.
customer-name loan-number
Jones L-170
Smith L-230
Hayes L-155
Note: borrower information missing for L-260 and loan
information missing for L-155
select customer-name
from (depositor natural full outer join borrower)
where account-number is null or loan-number is null