3. ORM Pros & Cons
• Pros
• Transforms result set into a data structure, which can
be easily handled in the programs
• Handles many kinds of RDBMS transparently with same
code
• Cons
• Hard to control generating SQL
• Hard to use for the complicated requirements
4. Relational Database
• MySQL, PostgreSQL, Oracle Database, DB2,
MSSQL, SQLite, etc.
• Relational database is based on set theory.
5. Set Theory
• Set theory is the branch of mathematical logic
that studies sets, which are collections of objects.
6. Terms of Set Theory
• Member
• Subset
• Union
• Intersection
• Difference
• Tuple
• Selection
• Projection
7. Member
• A fundamental binary relation between an object o
and a set A.
• o is a member of A, denoted o ∈ A.
• Example: 2 ∈ {1, 2, 3}
1
2
3
8. Subset
• If all the members of set A are also members of set B.
• A is a subset of B, denoted A ⊆ B.
• Example: {1, 2} ⊆ {1, 2, 3, 4}
1 2
3
4
9. Union
• Union creates a new set, which contains all the members
of 2 sets.
• Union of the sets A and B, denoted A ∪ B.
• Example: {1, 2, 3} ∪ {2, 3, 4} = {1, 2, 3, 4}
2
31 4
10. Intersection
• Intersection creates a new set, which contains common
members between 2 sets.
• Intersection of the sets A and B, denoted A ∩ B.
• Example: {1, 2, 3} ∩ {2, 3, 4} = {2, 3}
2
31 4
11. Difference
• Difference creates a new set, which contains remaining
members in a set after subtracting the set from another set.
• Difference of the sets A and B, denoted A ー B.
• Example: {1, 2, 3} ー {2, 3, 4} = {1}
2
31 4
12. Tuple
• is a kind of data type, which represents finite
ordered list of elements
• Example: {(a, 1), (b,2), (c, 3)}
(b,2)
(c,3)
(a,1)
13. Selection
• Selecting all tuples from set T1 if a tuple matches
the conditions. Then creates T2 as new set.
• σ Age >= 34 ^ Weight <= 50 (Person)
(b,2)
(c,3)
(a,1)
(c,3)
(a,1)
T1 T2
14. Projection
• Picking a sub-set of all available columns
• Modifying an attribute’s value
• Π Age,Weight (Person)
(b,2)
(c,3)
(a,1)
f(x)
f(x)
f(x)
4
6
2
15. Correspondence
between set theory and RDB
• Table is a set of objects
• Record is a tuple
• SELECT is projection
• WHERE is selection
16. INNER JOIN
• is an expression, which joins two tables with conditions.
• returns records, which exactly match the conditions.
18. LEFT OUTER JOIN
• is an expression, which joins two tables with conditions.
• If conditions are matched, it returns values on both side of the tables
• If not, it returns values in the left side of the table with NULL on the right side
19. RIGHT OUTER JOIN
• is an expression, which joins two tables with conditions.
• If conditions are matched, it returns values on both side of the tables
• If not, it returns values in the right side of the table with NULL on the left side
20. FULL OUTER JOIN
• is an expression, which joins two tables with conditions.
• If conditions are matched, it returns values on both side of the tables
• If not, it returns values in the either side of the table with NULL on the other side
21. UNION (ALL)
• is a kind of operator, which combines result of
two or more SELECT statements.
22. Question 1
• Try to write SQL, which realises following Venn
diagram.
• Use setA and setB tables.
23. Question 2
• Try to write SQL, which realises following Venn
diagram.
• Use setA and setB tables.
24. Question 3
• Try to fetch students, who belong to as same course as
Mike.
• Make sure to exclude Mike from the result.
25. CASE
SELECT
product_name
, amount
, CASE
WHEN amount = 0
THEN 'Nothing. You should order ASAP!'
WHEN amount > 0 AND amount <= 10
THEN 'You have enough stock.'
WHEN amount > 10
THEN 'Too much. It’ll be dead stock.'
WHEN amount IS NULL
THEN 'Something went wrong on the system..'
ELSE 'Amount is minus. Check the stock.'
END AS suggestion
FROM stock;
27. Question 4
• Try to count number of male and number of
female respectively.
gender number
male 4
female 2
number_of_male number_of_female
4 2
1)
2)
28. Question 5
• Try to show ranking of the students by their
scores ordered by score descending.
name score rank
Beth 95 1
Mike 90 2
Jack 80 3
John 80 3
Tina 80 3
Nick 75 6
29. Question 6
• Try to show ranking of the students by their
scores ordered by score descending.
name score rank
Beth 95 1
Mike 90 2
Jack 80 3
John 80 3
Tina 80 3
Nick 75 4
Notes de l'éditeur
Oracle database
MSSQL server
SELECT a.name, b.name
FROM setA AS a
LEFT OUTER JOIN setB AS b
ON b.name = a.name
UNION
SELECT a.name, b.name
FROM setA AS a
RIGHT OUTER JOIN setB AS b
ON b.name = a.name
;
SELECT a.name, b.name
FROM setA AS a
LEFT OUTER JOIN setB AS b
ON b.name = a.name
WHERE b.name IS NULL
;
exclusive disjunction / exclusive or
SELECT a.name, b.name
FROM setA AS a
LEFT OUTER JOIN setB AS b
ON b.name = a.name
WHERE b.name IS NULL
UNION
SELECT a.name, b.name
FROM setA AS a
RIGHT OUTER JOIN setB AS b
ON b.name = a.name
WHERE a.name IS NULL
;
SELECT b.name
FROM students AS a
INNER JOIN students AS b
ON a.course = b.course
WHERE a.name <> b.name
AND a.name = 'Mike'
;
SELECT
product_name
, amount
, CASE
WHEN amount = 0 THEN 'Nothing. You should order ASAP!'
WHEN amount > 0 AND amount < 10 THEN 'You have enough stock.'
WHEN amount > 10 THEN 'Too much. You should sell it or it will be dead stock.'
WHEN amount IS NULL THEN 'Something went wrong on the system..'
ELSE 'Amount is minus. Check the stock.'
END AS suggestion
FROM stock;
SELECT
CASE
WHEN gender = 1 THEN ‘male’
WHEN gender = 2 THEN ‘female'
END AS gender,
COUNT(gender) AS number FROM students GROUP BY gender;
SELECT
SUM(CASE WHEN gender = 1 THEN 1 ELSE 0 END) AS number_of_male
, SUM(CASE WHEN gender = 2 THEN 1 ELSE 0 END) AS number_of_female
FROM students;
SELECT
name
, score
, (SELECT COUNT(score) FROM students AS s2 WHERE s2.score > s1.score) + 1 AS rank
FROM students AS s1
ORDER BY rank
SELECT
s1.name
, s1.score
, COUNT(s2.name) + 1 AS rank
FROM students AS s1
LEFT OUTER JOIN students AS s2
ON s1.score < s2.score
GROUP BY s1.name
ORDER BY s1.score DESC
SELECT
name
, score
, (SELECT COUNT(DISTINCT score) FROM students AS s2 WHERE s2.score > s1.score) + 1 AS rank
FROM students AS s1
ORDER BY rank
SELECT
s1.name
, s1.score
, COUNT(s2.name) + 1 AS rank
FROM students AS s1
LEFT OUTER JOIN (
SELECT
name
, score
FROM students
GROUP BY score
) AS s2
ON s1.score < s2.score
GROUP BY s1.name
ORDER BY s1.score DESC