SELECT BDATE, ADDRESS FROM EMPLOYEE WHERE FNAME='John' AND MINIT='B’ AND LNAME='Smith Query 1: Retrieve the birthdate and address of the employee whose name is 'John B. Smith'.
SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHERE DNAME='Research' AND DNUMBER=DNO Query 2: Retrieve the name and address of all employees who work for the 'Research' department.
SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN AND PLOCATION='Stafford' Query 3: For every project located in 'Stafford', list the project number, the controlling department number, and the department manager's last name, address, and birthdate.
(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 NAME='Smith') 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.
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 ) Query 5: Retrieve the name of each employee who has a dependent with the same first name as the employee.
SELECT E.FNAME, E.LNAME FROM EMPLOYEE E, DEPENDENT D WHERE E.SSN=D.ESSN AND E.FNAME=D.DEPENDENT_NAME Query 6: Retrieve the name of each employee who has a dependent with the same first name as the employee.
SELECT DNAME, LNAME, FNAME,PNAME FROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT WHERE DNUMBER=DNO AND SSN=ESSN AND PNO=PNUMBER ORDER BY DNAME, LNAME Query 7: Retrieve a list of employees and the projects each works in, ordered by the employee's department, and within each department ordered alphabetically by employee last name.
SELECT FNAME, LNAME FROM EMPLOYEE WHERE ADDRESS LIKE'%Houston,TX%' Query 8: Retrieve all employees whose address is in Houston, Texas. Here, the value of the ADDRESS attribute must contain the substring 'Houston,TX‘ in it.
SELECT FNAME, LNAME FROM EMPLOYEE WHERE ( (SELECT PNO FROM WORKS_ON WHERE SSN=ESSN) CONTAINS (SELECT PNUMBER FROM PROJECT WHERE DNUM=5)) Query 9: Retrieve the name of each employee who works on all the projects controlled by department number 5.
SELECT FNAME, LNAME FROM EMPLOYEE WHERE NOT EXISTS (SELECT * FROM DEPENDENT WHERE SSN=ESSN) Query 10: Retrieve the names of employees who have no dependents.
SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY) FROM EMPLOYEE, DEPARTMENT WHERE DNO=DNUMBER AND DNAME='Research' Query 11: Find the maximum salary, the minimum salary, and the average salary among employees who work for the 'Research‘ department.
SELECT DNO, COUNT (*),AVG (SALARY) FROM EMPLOYEE GROUP BY DNO Query 12: For each department, retrieve the department number, the number of employees in the department, and their average salary.
SELECT PNUMBER, PNAME, COUNT (*) FROM PROJECT, WORKS_ON WHERE PNUMBER=PNO GROUP BY PNUMBER, PNAME Query 13: For each project, retrieve the project number, project name, and the number of employees who work on that project.
SELECT PNUMBER, PNAME,COUNT(*) FROM PROJECT, WORKS_ON WHERE PNUMBER=PNO GROUP BY PNUMBER, PNAME HAVING COUNT (*) > 2 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.
CREATE ASSERTION SALARY_CONSTRAINT CHECK ( NOT EXISTS ( SELECT * FROM EMPLOYEE E, EMPLOYEE M, DEPARTMENT D WHERE E.Salary>M.Salary AND E.Dno=D.Dnumber AND D.Mgr_ssn=M.Ssn ) ) Query 15: Specify the constraint that the salary of an employee must not greater than the salary of the manager of the department that the employee works for.
Query 16: Find the average grade of all ”CS” curriculum students with respect to different semesters. SELECT T.Semester, AVG(T.Grade) FROM Take AS T, Student AS S WHERE (S.SID = T.SID) AND (S.Curriculum = "CS") GROUP BY T.Semester
Query 17: Find all courses that are offered at least once every year. SELECT C.Cname FROM Course AS C WHERE NOT EXISTS(SELECT distinct(O1.Year) FROM Offer AS O1 WHERE O1.Year NOT IN (SELECT O2.Year FROM Offer AS O2 WHERE (O2.CID = C.CID))
Query 18: Find all faculties who taught a course in which the average grade for CS students was lower than the other students.
SELECT F.Fname FROM Faculty AS F WHERE F.FID IN (SELECT T1.FID FROM Take AS T1, Student AS S1 WHERE (T1.SID=S1.SID) AND (S1.Curriculum="CS") GROUP BY T1.FID HAVING Avg(T1.Grade) "CS") AND (T1.FID = T2.FID) AND (T1.CID = T2.CID) AND (T1.Semester = T2.Semester) AND (T1.Year = T2.Year) GROUP BY T2.FID)
Query 19: Find the number and department identifier of all courses in which no student ever got an ’F’. SELECT C.course_number, C.dept_id FROM COURSE C WHERE NOT EXISTS (SELECT * FROM TRANSCRIPT T, SECTION S WHERE (T.grade = ’F’) AND (T.section_id =S.section_id) AND (S.course_number =C.course_number) AND (S.dept_id = C.dept_id)
Query 20: Find the id of all sections of courses offered by department ”Computer Science” in the ”Fall99” semester. SELECT S.section_id FROM section S, department D WHERE S.dept_id = D.dept_id AND D.department_name = "Computer Science" AND S.semester = "Fall99"
Query 21: Find the id of all sections that a student named ”Kenny” is taking in ”Fall99” semester. SELECT T.section_id FROM section S, transcript T, student St WHERE St.sid = T.sid AND S.section_id = T.section_id AND St.firstname = "Kenny" AND S.semester "Fall99"
Query 22: Find the first name, last name and the department name for all instructors who are teaching at least three sections of a single course in semester ’Fall 99’. SELECT I.firstname, I.lastname, D.department_name FROM INSTRUCTOR I, DEPARTMENT D WHERE (I.dept_id = D.dept_id) AND EXISTS (SELECT S.course_number, S.dept_id FROM SECTION S WHERE (S.semester = ’Fall 99’) AND (S.instructor_id = I.instructor_id) GROUP BY S.course_number, S.dept_id HAVING count(S.section_id) >= 3)
Query 23: For all different courses in the curriculum, find the total number of students in that class for each different semester (regardless of their sections). SELECT SC.course_number, SC.dept_id, SC.semester, count(TR.sid) FROM SECTION SC, TRANSCRIPT TR WHERE SC.section_id = TR.section_id GROUP BY SC.course_number, SC.dept_id, SC.semester
Query 24: Find the average number of students in each different course (identified by course number and dept id), average over different semesters. Find for each course number of students for different semesters and then take the average. SELECT S.course_number, S.dept_id, count(S.sid)/count(DISTINCT S.semester) FROM section S, transcript T WHERE S.section_id = T.section_id GROUP BY S.course_number, S.dept_id
Query 25: Set the grade of student named ”Kenny” for course number 111, dept id 15 offered in ”Fall99” to ”F”. UPDATE transcript SET grade = ’F’ WHERE sid IN (SELECT S.sid FROM student S WHERE S.firstname = ’Kenny’) AND section_id IN (SELECT Sc.section_id FROM section Sc WHERE Sc.course_number = 111 AND Sc.dept_id = 15 AND Sc.semester = ’Fall99’)
Query 26: Insert a tuple into transcript indicating that ”Kenny” is taking course number 111, dept id 15 in ”Fall99”. INSERT INTO transcript(sid, section_number) SELECT DISTINCT S.sid, Sc.section_number FROM student S, section Sc WHERE S.firstname = ’Kenny’ AND Sc.dept_id = 15 AND Sc.course_number = 111 AND Sc.semester = ’Fall99’