Module 5 Short
Module 5 Short
CS 5200
Dr. Tehmina Amjad
Basic SQL
• Catalog
• Named collection of schemas in an SQL environment
• SQL also has the concept of a cluster of catalogs.
Query 8. For each employee, retrieve the employee’s first and last name and the first and last name
of his or her immediate supervisor.
• SELECT E.Fname, E.Lname, S.Fname, S.Lname
FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.Super_ssn=S.Ssn;
• Recommended practice to abbreviate names and to prefix same or similar
attribute from multiple tables.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe
31
Aliasing, Renaming and Tuple Variables
(contd.)
• The attribute names can also be renamed
EMPLOYEE AS E(Fn, Mi, Ln, Ssn, Bd, Addr, Sex,
Sal, Sssn, Dno)
• Note that the relation EMPLOYEE now has a variable name E which
corresponds to a tuple variable
• The “AS” may be dropped in most SQL implementations
• Query 13. Show the resulting salaries if every employee working on the
‘ProductX’ project is given a 10 percent raise.
• In this request, the modified SALARY value depends on the original SALARY value
in each tuple
• The reference to the SALARY attribute on the right of = refers to the old
SALARY value before modification
• The reference to the SALARY attribute on the left of = refers to the new
SALARY value after modification
SELECT
SELECT FROM
WHERE
FROM GROUP BY
WHERE
SELECT
FROM
WHERE group by 1;
WHERE
+------+-------+------+---------+-------+-----------+
| sid | sname | dno | dnumber | dname | dlocation |
+------+-------+------+---------+-------+-----------+
| 1 | Lee | 20 | 20 | CS | SJ |
| 2 | Patel | 30 | 30 | DS | SF |
+------+-------+------+---------+-------+-----------+
mysql> select * from student left outer join dept on dno = dnumber;
+------+-------+------+---------+-------+-----------+
| sid | sname | dno | dnumber | dname | dlocation |
+------+-------+------+---------+-------+-----------+
| 1 | Lee | 20 | 20 | CS | SJ |
| 2 | Patel | 30 | 30 | DS | SF |
| 3 | Peng | 40 | NULL | NULL | NULL |
+------+-------+------+---------+-------+-----------+
mysql> select * from student right outer join dept on dno = dnumber;
+------+-------+------+---------+-------+-----------+
| sid | sname | dno | dnumber | dname | dlocation |
+------+-------+------+---------+-------+-----------+
| NULL | NULL | NULL | 10 | EE | NY |
| 1 | Lee | 20 | 20 | CS | SJ |
| 2 | Patel | 30 | 30 | DS | SF |
+------+-------+------+---------+-------+-----------+
M y S QL !
mysql> select * from student p p or t
fulled inouter join dept on dno = dnumber;
Not Su
mysql> select * from student left outer join dept on dno = dnumber
-> union
-> select * from student right outer join dept on dno = dnumber;
+------+-------+------+---------+-------+-----------+
| sid | sname | dno | dnumber | dname | dlocation |
+------+-------+------+---------+-------+-----------+
| 1 | Lee | 20 | 20 | CS | SJ |
| 2 | Patel | 30 | 30 | DS | SF |
| 3 | Peng | 40 | NULL | NULL | NULL |
| NULL | NULL |Copyright
NULL© 2016
| Ramez Elmasri
10and|Shamkant
EE B. Navathe
| NY |
87
+------+-------+------+---------+-------+-----------+
Example: LEFT OUTER JOIN
ALTERNATE SYNTAX:
SELECT E.Lname , S.Lname
FROM EMPLOYEE E, EMPLOYEE S
WHERE E.Super_ssn = S.Ssn (+)
M y S QL !
p or te d in
p
Not Su g Oracle !!
sin
Only u
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe
88
Multiway JOIN in the FROM clause
• FULL OUTER JOIN – combines result if LEFT and RIGHT OUTER JOIN
• Can nest JOIN specifications for a multiway join:
91
GROUP BY
Result of Having
SELECTDNO, COUNT(*), AVG(SALARY)
FROM EMPLOYEE
GROUP BY DNO
HAVING COUNT(*) > 3;
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe
99
Combining the WHERE and the HAVING
Clause
• Consider the query: we want to count the total number of employees whose
salaries exceed $40,000 in each department, but only for departments where
more than five employees work.
• INCORRECT QUERY:
SELECT Dno, COUNT (*)
FROM EMPLOYEE
WHERE Salary>40000
GROUP BY Dno
HAVING COUNT (*) > 5;