0% found this document useful (0 votes)
7 views9 pages

DTB

The document contains a series of SQL queries designed to extract specific information from databases related to employees, departments, projects, products, customers, and students. It includes queries for filtering data based on various criteria such as gender, project involvement, sales dates, and academic performance. Each query is structured to retrieve relevant fields from multiple tables using JOINs and subqueries.

Uploaded by

nguyenlinhthuy78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

DTB

The document contains a series of SQL queries designed to extract specific information from databases related to employees, departments, projects, products, customers, and students. It includes queries for filtering data based on various criteria such as gender, project involvement, sales dates, and academic performance. Each query is structured to retrieve relevant fields from multiple tables using JOINs and subqueries.

Uploaded by

nguyenlinhthuy78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Thẻ 1

Ex 3.3.
EMPLOYEE(SSN, EName, Sex, Salary, Dnumber, Super_Ssn)
DEPARTMENT(DNumber , Dname, Mgr_SSN, Mgr_StartDate)
PROJECT(PNumber, Pname, Plocation, DNumber)
WORKS_ON(Ssn, PNumber, Hours)

1. Find the ID numbers and names of the female employees of the "Administration"
department.
SELECT Employee.SSN, Employee.FName
FROM Employee, Department
WHERE Employee.Dnumber = Department.DNumber
AND Employee.Sex = 'Female'
AND Department.Dname = 'Administration';

2. List the projects that the "Administration" department controls


SELECT Project.PNumber, Project.PName
FROM Project,Department
WHERE Project.DNumber = Department.DNumber
AND Department.Dname = 'Administration';

3. List the ID numbers and names of employees working on project code "P2022_01".
SELECT Employee.SSN, Employee.LName
FROM Employee, Works_On, Project
WHERE Employee.SSN = Works_On.SSN
AND Works_On.PNumber = Project.PNumber
AND Project.PName = 'P2022_01';

4. Find the ID numbers and names of employees in the "Administration" department


working on project code "P2022_01"
SELECT Employee.SSN, Employee.LName
FROM Employee, Department, Works_On, Project
WHERE Employee.SSN = Works_On.SSN
AND Works_On.PNumber = Project.PNumber
AND Employee.Dnumber = Department.DNumber
AND Department.Dname = 'Administration'
AND Project.Pname = 'P2022_01';

5. Find the number and name of the manager of the "Administration" department.
SELECT Employee.SSN, Employee.LName
FROM Employee, Department
WHERE Employee.SSN = Department.Mgr_SSN
AND Department.DName = 'Administration';

6. List the codes and names of employees who are supervisors

SELECT Employee.SSN, Employee.LName


FROM Employee
WHERE Employee.SSN IN (SELECT DISTINCT Super_SSN
FROM Employee WHERE Super_SSN IS NOT NULL);

7. List the codes and names of employees in the "Administration" department who are
supervisors.
SELECT Employee.SSN, Employee.EName
FROM Employee, Department
WHERE Employee.SSN = Department.Mgr_SSN
AND Department.Dname = 'Administration'
AND Employee.SSN IN (SELECT DISTINCT Super_SSN FROM Employee WHERE
Super_SSN IS NOT NULL);

8. Find the ID numbers and names of employees in the "Administration" department


who are not working on any project.
SELECT Employee.SSN, Employee.LName
FROM Employee, Department
WHERE Employee.Dnumber = Department.DNumber
AND Department.DName = 'Administration'
AND Employee.SSN NOT IN (
SELECT SSN
FROM Works_On );

9. List the ID numbers and names of employees in the "Administration" department


working on project "P2023-02" but not working on project "P2023-01".
SELECT Employee.SSN, Employee.LName
FROM Employee, Department, Works_On, Project
WHERE Employee.Dnumber = Department.DNumber
AND Department.Dname = 'Administration'
AND Works_On.SSN = Employee.SSN
AND Works_On.PNumber = Project.PNumber
AND Project.Pname = 'P2023-02'
AND Employee.SSN NOT IN (
SELECT SSN
FROM Works_On, Project
WHERE Works_On.PNumber = Project.PNumber
AND Project.PName = 'P2023-01'
);

11. Find the ID number and name of the employee who is the manager of the
"Administration" department.
SELECT Employee.SSN, Employee.LName
FROM Employee, Department
WHERE Employee.SSN = Department.Mgr_SSN
AND Department.Dname = 'Administration';

12. Find the ID number and name of the employee with the highest salary in the
"Administration" department.
SELECT Employee.SSN, Employee.LName
FROM Employee, Department
WHERE Employee.Dnumber = Department.DNumber
AND Department.Dname = 'Administration'
AND Employee.Salary = (
SELECT MAX(Salary)
FROM Employee, Department
WHERE Employee.Dnumber = Department.DNumber
AND Department.Dname = 'Administration'
);

13. Make a list of statistics on total of working hours (in 1 week on projects) of each
employee, the list includes the columns: Employee's code, Employee's name, Total
hours.
SELECT Employee.SSN, Employee.LName, SUM(Works_On.Hours) AS Total_Hours
FROM Employee, Works_On
WHERE Employee.SSN = Works_On.Ssn
GROUP BY Employee.SSN, Employee.LName;

14. Make a list of statistics on the average salary in each Department. The list includes
the columns: Department's code, Department's name, Average salary.
SELECT Department.DNumber, Department.Dname, AVG(Employee.Salary) AS
Average_Salary
FROM Department, Employee
WHERE Employee.Dnumber = Department.DNumber
GROUP BY Department.DNumber, Department.Dname;

15. Find the department with the largest number of female employees.
SELECT TOP 1 Department.DName
FROM Department, Employee
WHERE Employee.Dnumber = Department.DNumber
AND Employee.Sex = 'Female'
GROUP BY Department.DName
ORDER BY COUNT(Employee.SSN) DESC;

Ex 3.2.
PRODUCTS(ProID, ProName, Price, InStock)
CUSTOMERS(CusID, CusName, Phone, Email, Birthday)
ORDERS(OrdNum, CusID, OrdDate, PurchaseAmt)
ORDERDETAILS( OrdNum, ProID, Quantity, UnitPrice, TotalAmount)

16. List the codes and names of products sold in October 2023.
SELECT ProID, ProName
FROM PRODUCTS
WHERE ProID IN (
SELECT DISTINCT ProID
FROM ORDERDETAILS
WHERE OrdNum IN (
SELECT OrdNum
FROM ORDERS
WHERE OrdDate BETWEEN #2023-10-01# AND #2023-10-31#
)
);

17. List the codes and names of products NOT sold in October 2023.
SELECT ProID, ProName
FROM PRODUCTS
WHERE ProID NOT IN (
SELECT DISTINCT ProID
FROM ORDERDETAILS
WHERE OrdNum IN (
SELECT OrdNum
FROM ORDERS
WHERE OrdDate BETWEEN #2023-10-01# AND #2023-10-31#
)
);

18. List the names and phone numbers of customers who purchased both products
'P1' and 'P2' on the same invoice in October 2022.
SELECT DISTINCT Customers.CusName, Customers.Phone
FROM CUSTOMERS AS Customers
WHERE Customers.CusID IN (
SELECT Orders.CusID
FROM ORDERS AS Orders
WHERE Orders.OrdNum IN (
SELECT OrderDetails.OrdNum
FROM ORDERDETAILS AS OrderDetails
WHERE OrderDetails.ProID = 'P1'
)
AND Orders.OrdNum IN (
SELECT OrderDetails.OrdNum
FROM ORDERDETAILS AS OrderDetails
WHERE OrderDetails.ProID = 'P2'
)
AND Orders.OrdDate BETWEEN #2022-10-01# AND #2022-10-31#
);

19. Find the code and name of the customer who purchased the product code 'P1'
with the largest quantity (per a invoice) on the date '10/10/2023'.
SELECT Customers.CusID, Customers.CusName
FROM CUSTOMERS AS Customers
WHERE Customers.CusID IN (
SELECT Orders.CusID
FROM ORDERS AS Orders
WHERE Orders.OrdNum IN (
SELECT ORDERDETAILS.OrdNum
FROM ORDERDETAILS AS ORDERDETAILS
WHERE ORDERDETAILS.ProID = 'P1'
AND ORDERDETAILS.Quantity = (
SELECT MAX(ORDERDETAILS.Quantity)
FROM ORDERDETAILS AS ORDERDETAILS
WHERE ORDERDETAILS.ProID = 'P1'
AND ORDERDETAILS.OrdNum = Orders.OrdNum
)
)
AND Orders.OrdDate = #2023-10-10#
);

20. Displays the day with the highest sales revenue.


SELECT TOP 1 ORDERS.OrdDate, SUM(ORDERDETAILS.TotalAmount) AS TotalSales
FROM ORDERS
INNER JOIN ORDERDETAILS ON ORDERS.OrdNum = ORDERDETAILS.OrdNum
GROUP BY ORDERS.OrdDate
ORDER BY SUM(ORDERDETAILS.TotalAmount) DESC;

Ex 3.1.
Student (StdID, StdName, StdBirthday, StdMajor, StdClass, StdGPA)
Course (CrsNo, CrsDesc, CrsCredit)
Lecturer (LecID, LecName, LecDept, LecSalary, LecSupervisor)
Offering (OfferNo, CrsNo, OffTerm, OffYear, OffLocation, OffTime, OffDay,LecID)
Enrollment (OfferNo, StdID, EnrGrade)

13. List the ID and name of students who have a grade >= 3.5 in a course offering
SELECT Student.StdID, Student.StdName
FROM Student, Enrollment
WHERE Student.StdID = Enrollment.StdID
AND Enrollment.EnrGrade >= 3.5;

14. List the ID and name of students who have a grade >= 3.5 in a course offering in
term 2 year 2023.
SELECT StdID, StdName
FROM Student
WHERE StdID IN (
SELECT StdID
FROM Enrollment
WHERE EnrGrade >= 3.5
AND OfferNo IN (
SELECT OfferNo
FROM Offering
WHERE OffTerm = '20232' AND OffYear ='2023'
)
);
15. List codes and full names of students with no failed courses. Sort the list in
ascending order of student names.
SELECT StdID, StdName
FROM Student
WHERE StdID NOT IN (
SELECT StdID
FROM Enrollment
WHERE EnrGrade < 3.0
)
ORDER BY StdName ASC;

16. List codes and full names of students with no failed courses in term 2 year 2023.
SELECT Student.StdID, Student.StdName
FROM Student
WHERE StdID NOT IN (
SELECT Enrollment.StdID
FROM Enrollment, Offering
WHERE Enrollment.OfferNo = Offering.OfferNo
AND Offering.OffTerm = 2
AND Offering.OffYear = 2023
AND Enrollment.EnrGrade < 2.0
);

17. Find courses that have more credits than the credits of course M13090.
SELECT CrsNo, CrsDesc, CrsCredit
FROM Course
WHERE CrsCredit > (
SELECT CrsCredit
FROM Course
WHERE CrsNo = 'MI3090'
);

18. Find the code numbers of the students who got the highest grade in the course
'M13090'.
SELECT StdID
FROM Enrollment
WHERE OfferNo IN (
SELECT OfferNo
FROM Offering
WHERE CrsNo = 'MI3090'
)
AND EnrGrade = (
SELECT MAX(EnrGrade)
FROM Enrollment
WHERE OfferNo IN (
SELECT OfferNo
FROM Offering
WHERE CrsNo = 'MI3090'
)
);

19. List students who got the highest grade in courses in semester 1, 2023.
SELECT StdID, StdName
FROM Student
WHERE StdID IN (
SELECT StdID
FROM Enrollment
WHERE OfferNo IN (
SELECT OfferNo
FROM Offering
WHERE OffTerm = 'Semester 1' AND OffYear = 2023
)
AND EnrGrade = (
SELECT MAX(EnrGrade)
FROM Enrollment
WHERE OfferNo IN (
SELECT OfferNo
FROM Offering
WHERE OffTerm = 'Semester 1' AND OffYear = 2023
)
)
);

20. Retrieve the code numbers of the students who got the highest grade in the
course 'Database' (CrsDesc = 'Database').
SELECT StdID
FROM Enrollment
WHERE OfferNo IN (
SELECT OfferNo
FROM Offering
WHERE CrsNo IN (
SELECT CrsNo
FROM Course
WHERE CrsDesc = 'Database'
)
)
AND EnrGrade = (
SELECT MAX(EnrGrade)
FROM Enrollment
WHERE OfferNo IN (
SELECT OfferNo
FROM Offering
WHERE CrsNo IN (
SELECT CrsNo
FROM Course
WHERE CrsDesc = 'Database'
)
)
);

21. Find the names of the lecturers who are supervisors.


SELECT Lecturer.LecName
FROM Lecturer
WHERE Lecturer.LecSupervisor IS NOT NULL;

22. Find the names of the lecturers who are supervisors of lecturer 'M1000'.
SELECT Lecturer.LecName
FROM Lecturer
WHERE Lecturer.LecID = (
SELECT LecSupervisor
FROM Lecturer
WHERE LecID = ‘M1000’
);

23. Find classes that have a larger student population than the student population of
class 'EM1'.
SELECT StdClass
FROM Student
GROUP BY StdClass
HAVING COUNT(StdID) > (
SELECT COUNT(StdID)
FROM Student
WHERE StdClass = 'EM1'
);

You might also like