MUHAMMAD FARHAN 13500
DBMS ACTIVITY
Find employees whose salary is higher than the average salary of all employees:
SELECT name
FROM Employees
WHERE salary > (SELECT AVG(salary) FROM Employees);
List all departments where the number of employees is greater than the number of employees in
the "Sales" department:
SELECT department_name
FROM Departments
WHERE (SELECT COUNT(*) FROM Employees WHERE Employees.department_id =
Departments.department_id)
> (SELECT COUNT(*) FROM Employees WHERE department_id = (SELECT department_id FROM
Departments WHERE department_name = 'Sales'));
Display order details along with customer names by joining "Orders" and "Customers":
SELECT Orders.order_id, Orders.order_date, Customers.customer_name
FROM Orders
JOIN Customers ON Orders.customer_id = Customers.customer_id;
Retrieve all employees, including those without assigned departments (LEFT JOIN):
SELECT Employees.employee_name, Departments.department_name
FROM Employees
LEFT JOIN Departments ON Employees.department_id = Departments.department_id;
List employees and their managers using a self-join:
SELECT e1.employee_name AS Employee, e2.employee_name AS Manager
FROM Employees e1
LEFT JOIN Employees e2 ON e1.manager_id = e2.employee_id;
Find employees in "HR" or "IT" departments, excluding those with salary > 80,000:
SELECT employee_name
FROM Employees
WHERE department_id IN (SELECT department_id FROM Departments WHERE department_name
IN ('HR', 'IT'))
AND salary <= 80000;
Retrieve products with a price between 50 and 200:
SELECT product_name
FROM Products
WHERE price BETWEEN 50 AND 200;
Display total sales for each product where total sales exceed 5000:
SELECT product_name, SUM(order_quantity * price) AS total_sales
FROM OrderDetails
JOIN Products ON OrderDetails.product_id = Products.product_id
GROUP BY product_name
HAVING SUM(order_quantity * price) > 5000;
Find the average salary of employees in each department:
SELECT Departments.department_name, AVG(Employees.salary) AS avg_salary
FROM Employees
JOIN Departments ON Employees.department_id = Departments.department_id
GROUP BY Departments.department_name;
Find all products ordered by customers in "New York":
SELECT Products.product_name
FROM Orders
JOIN Customers ON Orders.customer_id = Customers.customer_id
JOIN OrderDetails ON Orders.order_id = OrderDetails.order_id
JOIN Products ON OrderDetails.product_id = Products.product_id
WHERE Customers.city = 'New York';