Intro To SQL
Intro To SQL
SELECT *
FROM EMPLOYEE
WHERE DNO=5
SELECT *
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME='Research' AND
DNO=DNUMBER
USE OF DISTINCT
• SQL does not treat a relation as a set; duplicate tuples can
appear
• To eliminate duplicate tuples in a query result, the keyword
DISTINCT is used
• For example, the result of the first query may have duplicate
SALARY values whereas the second one does not have any
duplicate values
SELECT SALARY
FROM EMPLOYEE
– In this case, the grouping and functions are applied after the joining
of the two relations
THE HAVING-CLAUSE
• Sometimes we want to retrieve the values of
these functions for only those groups that
satisfy certain conditions
• The HAVING-clause is used for specifying a
selection condition on groups (rather than on
individual tuples)
THE HAVING-CLAUSE (contd.)
Query: For each project on which more than
two employees work, retrieve the project
number, project name, and the number of
employees who work on that project.
SELECT PNUMBER, PNAME,
COUNT(*)
FROMPROJECT, WORKS_ON
WHERE PNUMBER=PNO
GROUP BY PNUMBER, PNAME
HAVING COUNT (*) > 2