MC536/MC526 Profs. Anderson Rocha e André Santanché: Based On Original By: Babak Bagheri Hariri
MC536/MC526 Profs. Anderson Rocha e André Santanché: Based On Original By: Babak Bagheri Hariri
MC536/MC526 Profs. Anderson Rocha e André Santanché: Based On Original By: Babak Bagheri Hariri
Queries in SQL
Basic form of the SQL SELECT statement is called a mapping or a SELECT-FROM-WHERE block SELECT <attribute list> FROM <table list> WHERE <condition>
<attribute list> is a list of attribute names whose values are to be retrieved by the query <table list> is a list of the relation names required to process the query <condition> is a conditional (Boolean) expression that identifies the tuples to be retrieved by the query
Queries (i)
Query 0: Retrieve the birthdate and address of the employee whose name is 'John B. Smith'. Q0: SELECT BDATE, ADDRESS FROM EMPLOYEE WHERE FNAME='John' AND MINIT='B AND LNAME='Smith
Query 1: Retrieve the name and address of all employees who work for the 'Research' department. Q1: SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHERE DNAME='Research' AND DNUMBER=DNO
Queries (ii)
Query 2: For every project located in 'Stafford', list the project number, the controlling department number, and the department manager's last name, address, and birthdate. Q2: SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN AND PLOCATION='Stafford'
Query 3: For each employee, retrieve the employee's name, and the name of his or her immediate supervisor.
Q3: SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME FROM EMPLOYEE E S WHERE E.SUPERSSN=S.SSN
Queries (iii)
Query 4: Make a list of all project numbers for projects that involve an employee whose last name is 'Smith' as a worker or as a manager of the department that controls the project. Q4: (SELECT PNAME FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN AND LNAME='Smith') UNION (SELECT PNAME FROM PROJECT, WORKS_ON, EMPLOYEE WHERE PNUMBER=PNO AND ESSN=SSN AND LNAME='Smith')
Queries (iv)
The comparison operator IN compares a value v with a set (or multi-set) of values V, and evaluates to TRUE if v is one of the elements in V
Query 5: Retrieve the name of each employee who has a dependent with the same first name as the employee. Q5: SELECT E.FNAME, E.LNAME FROM EMPLOYEE AS E WHERE E.SSN IN (SELECT ESSN FROM DEPENDENT WHERE ESSN=E.SSN AND E.FNAME=DEPENDENT_NAME) SELECT E.FNAME, E.LNAME FROM EMPLOYEE E, DEPENDENT D WHERE E.SSN=D.ESSN AND E.FNAME=D.DEPENDENT_NAME
Q5A:
Q5B:
SELECT FNAME, LNAME FROM EMPLOYEE WHERE EXISTS (SELECT * FROM DEPENDENT WHERE SSN=ESSN AND FNAME=DEPENDENT_NAME)
Queries (vi)
Query 6: Retrieve the social security numbers of all employees who work on project number 1, 2, or 3. Q6: SELECT DISTINCT ESSN FROM WORKS_ON WHERE PNO IN (1, 2, 3)
Queries (vii)
The CONTAINS operator compares two sets of values , and returns TRUE if one set contains all values in the other set (reminiscent of the division operation of algebra).
Query 7: Retrieve the name of each employee who works on all the projects controlled by department number 5. Q7: SELECT FNAME, LNAME FROM EMPLOYEE WHERE ( (SELECT PNO FROM WORKS_ON WHERE SSN=ESSN) CONTAINS (SELECT PNUMBER FROM PROJECT WHERE DNUM=5) )
Query 8: Retrieve the names of all employees who do not have supervisors. Q8: SELECT FNAME, LNAME FROM EMPLOYEE WHERE SUPERSSN IS NULL
Note: If a join condition is specified, tuples with NULL values for the join attributes are not included in the result
Can be written as: QTA: SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME FROM (EMPLOYEE E LEFT OUTER JOIN EMPLOYEES ON E.SUPERSSN=S.SSN)
Can be written as: Q9A: SELECT FNAME, LNAME, ADDRESS FROM (EMPLOYEE JOIN DEPARTMENT ON DNUMBER=DNO) WHERE DNAME='Research
Or as:
Q9B:
SELECT FNAME, LNAME, ADDRESS FROM (EMPLOYEE NATURAL JOIN DEPARTMENT AS DEPT(DNAME, DNO, MSSN, MSDATE) WHERE DNAME='Research
AGGREGATE FUNCTIONS
Include COUNT, SUM, MAX, MIN, and AVG
Query 10: Find the maximum salary, the minimum salary, and the average salary among all employees.
Q10:
Query 11: Find the maximum salary, the minimum salary, and the average salary among employees who work for the 'Research' department.
Q11:
SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY) FROM EMPLOYEE, DEPARTMENT WHERE DNO=DNUMBER AND DNAME='Research'
Group by
SQL has a GROUP BY-clause for specifying the grouping attributes, which must also appear in the SELECT-clause
Query 12: For each department, retrieve the department number, the number of employees in the department, and their average salary. Q12: SELECT DNO, COUNT (*), AVG (SALARY) FROM EMPLOYEE GROUP BY DNO Query 13: For each project, retrieve the project number, project name, and the number of employees who work on that project. Q13: SELECT PNUMBER, PNAME, COUNT (*) FROM PROJECT, WORKS_ON WHERE PNUMBER=PNO GROUP BY PNUMBER, PNAME
Query 14: 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. Q14: SELECT PNUMBER, PNAME, COUNT (*) FROM PROJECT, WORKS_ON WHERE PNUMBER=PNO GROUP BY PNUMBER, PNAME HAVING COUNT (*) > 2
SELECT <attribute list> FROM <table list> [WHERE <condition>] [GROUP BY <grouping attribute(s)>] [HAVING <group condition>] [ORDER BY <attribute list>]
The SELECT-clause lists the attributes or functions to be retrieved The FROM-clause specifies all relations (or aliases) needed in the query but not those needed in nested queries The WHERE-clause specifies the conditions for selection and join of tuples from the relations specified in the FROM-clause GROUP BY specifies grouping attributes
HAVING specifies a condition for selection of groups ORDER BY specifies an order for displaying the result of a query A query is evaluated by first applying the WHEREclause, then GROUP BY and HAVING, and finally the SELECT-clause
From Clause:
[ FROM { < table_source > } [ ,...n ] ] < table_source > ::= table_name [ [ AS ] table_alias ] [ WITH ( < table_hint > [ ,...n ] )] | view_name [ [ AS ] table_alias ] | rowset_function [ [ AS ] table_alias ] | OPENXML | derived_table [ AS ] table_alias [ ( column_alias [ ,...n ] ) ] | < joined_table > < joined_table > ::= < table_source > < join_type > < table_source > ON < search_condition > | < table_source > CROSS JOIN < table_source > | < joined_table > < join_type > ::= [ INNER | { { LEFT | RIGHT | FULL } [ OUTER ] } ] [ < join_hint > ] JOIN Arguments < table_source >
Having:
Compute Clause:
[ COMPUTE { { AVG | COUNT | MAX | MIN | STDEV | STDEVP | VAR | VARP | SUM } ( expression ) } [ ,...n ] [ BY expression [ ,...n ] ] ]
Compute
Row aggregate function AVG COUNT MAX MIN STDEV STDEVP Result Average of the values in the numeric expression Number of selected rows Highest value in the expression Lowest value in the expression Statistical standard deviation for all values in the expression |Statistical standard deviation for the population for all values in the expression
Total of the values in the numeric expression Statistical variance for all values in the expression Statistical variance for the population for all values in the expression
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: