DBMS Practice Questions
DBMS Practice Questions
1. Employee
2. EmployeeManager
3. Product
4. Order (representing orders made by customers)
5. Sales (representing sales made by employees)
6. Customer
1. Employee Table
Schema:
2. EmployeeManager Table
Schema:
• employee_id
• manager_id
employee_id manager_id
1 3
2 3
3 4
4 NULL
employee_id manager_id
5 4
3. Product Table
Schema:
4. Order Table
Schema:
Schema:
6. Customer Table
Schema:
customer_id customer_name
201 John Doe
202 Jane Smith
203 Bob Johnson
204 Alice Williams
205 Eve Adams
CREATE TABLE Employee (
);
);
);
order_id INT PRIMARY KEY, -- Primary key constraint to uniquely identify each
order
customer_id INT NOT NULL, -- Foreign key reference to Customer table, NOT
NULL to ensure it's provided
product_id INT NOT NULL, -- Foreign key reference to Product table, NOT
NULL to ensure it's provided
);
sale_id INT PRIMARY KEY, -- Primary key constraint to uniquely identify each
sale
employee_id INT NOT NULL, -- Foreign key reference to Employee table, NOT
NULL to ensure it's provided
);
);
Now let’s apply the above-defined complex queries to this sample data.
• Expected Result: No employees have more than one manager in the sample data.
2. Find Products That Have Never Been Ordered
• Expected Result: Employees 4 (David) and 3 (Charlie) have not made any sales.
5. List Customers Who Have Purchased All Products
• Expected Result: No customer has purchased all products in the sample data.
6. Find All Managers With No Direct Reports
• Expected Result: Department 101 has the most employees (Alice and Charlie).
8. List All Products Purchased by More Than 100 Customers
• Expected Result: This condition will not be met in the sample data as the number of
customers is less than 100.
9. Find Customers Who Have Only Ordered the Most Expensive Product
• Expected Result: Customers 201 and 204 have exclusively ordered the most expensive
product (Laptop).
10. List Employees Who Are Also Customers
• Expected Result: There are no employees who are also customers in this sample data.
Relational Algebra:
SQL:
Description: List all product names that have never been included in any order.
RA:
PRODUCT_ORDER = π_product_id(Order)
PRODUCT_NO_ORDER = Product - PRODUCT_ORDER RESULT =
π_product_name(PRODUCT_NO_ORDER)
SQL:
Description: Find the top 5 customers by the total amount of their purchases.
RA:
SALES = γ_customer_id; sum(amount)(Order)
TOP5_SALES = τ_sum(amount)(SALES)
RESULT = π_customer_id(ρ_TOP5_SALES(TOP5_SALES))
SQL:
Description: List all employee names who have never made a sale.
RA:
EMP_SALES = π_employee_id(Sales) EMP_NO_SALES = Employee - EMP_SALES RESULT =
π_employee_name(EMP_NO_SALES)
SQL:
SELECT employee_name FROM Employee WHERE employee_id NOT IN (SELECT DISTINCT
employee_id FROM Sales);
RA:
SQL:
SELECT employee_name
FROM Employee
WHERE employee_id NOT IN (SELECT DISTINCT employee_id FROM Sales);
RA:
SQL:
Description: Retrieve the names of all managers who do not have any direct reports
RA:
MANAGERS = π_manager_id(EmployeeManager) ALL_EMPLOYEES =
π_employee_id(Employee) MANAGERS_NO_REPORTS = MANAGERS - ALL_EMPLOYEES
RESULT = π_manager_name(MANAGERS_NO_REPORTS ⨝ Employee)
SQL:
Description: Identify the department that has the highest number of employees.
RA:
SQL:
Description: Find all product names that have been purchased by more than 100 unique customers.
RA:
SQL:
Q9. Find Customers Who Have Only Ordered the Most Expensive Product
Description: Retrieve the customer names who have exclusively ordered the most expensive
product.
RA:
SQL:
WITH MaxPriceProduct AS ( SELECT product_id FROM Product WHERE price = (SELECT
MAX(price) FROM Product) ) SELECT customer_id FROM Orders WHERE product_id IN
(SELECT product_id FROM MaxPriceProduct) GROUP BY customer_id HAVING
COUNT(DISTINCT product_id) = 1;
RA:
SQL: