0% found this document useful (0 votes)
17 views

DBMS Practice Questions

These are some basic practice questions for SQL

Uploaded by

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

DBMS Practice Questions

These are some basic practice questions for SQL

Uploaded by

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

Tutorial

Schema and Sample Data

Define the following Entities:

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:

• employee_id (Primary Key)


• employee_name
• department_id

employee_id employee_name department_id


1 Alice 101
2 Bob 102
3 Charlie 101
4 David 103
5 Eve 102

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:

• product_id (Primary Key)


• product_name
• price

product_id product_name price


101 Laptop 1000
102 Smartphone 800
103 Tablet 500
104 Monitor 300
105 Keyboard 100

4. Order Table

Schema:

• order_id (Primary Key)


• customer_id
• product_id
• amount

order_id customer_id product_id amount


1 201 101 1000
2 202 102 1600
3 203 103 1000
4 204 101 1000
5 205 104 300
6 201 102 800
5. Sales Table

Schema:

• sale_id (Primary Key)


• employee_id
• amount

sale_id employee_id amount


1 1 500
2 2 1500
3 1 1000
4 3 700
5 5 1200

6. Customer Table

Schema:

• customer_id (Primary Key)


• customer_name

customer_id customer_name
201 John Doe
202 Jane Smith
203 Bob Johnson
204 Alice Williams
205 Eve Adams
CREATE TABLE Employee (

employee_id INT PRIMARY KEY, -- Primary key constraint to uniquely identify


each employee

employee_name VARCHAR(100) NOT NULL, -- NOT NULL constraint to


ensure employee name is provided

department_id INT NOT NULL -- NOT NULL constraint to ensure department_id


is provided

);

CREATE TABLE EmployeeManager (

employee_id INT, -- Foreign key reference to Employee table

manager_id INT, -- Foreign key reference to Employee table

PRIMARY KEY (employee_id, manager_id), -- Composite primary key on both


columns to ensure uniqueness

FOREIGN KEY (employee_id) REFERENCES Employee(employee_id) ON


DELETE CASCADE, -- Foreign key constraint with CASCADE delete to remove
associations when an employee is deleted

FOREIGN KEY (manager_id) REFERENCES Employee(employee_id) --


Foreign key constraint to ensure manager exists in Employee table

);

CREATE TABLE Product (


product_id INT PRIMARY KEY, -- Primary key constraint to uniquely identify
each product

product_name VARCHAR(100) NOT NULL, -- NOT NULL constraint to ensure


product name is provided

price DECIMAL(10, 2) NOT NULL -- NOT NULL constraint to ensure price is


provided, using DECIMAL for monetary values

);

CREATE TABLE Orders (

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

amount DECIMAL(10, 2) NOT NULL, -- NOT NULL constraint to ensure


amount is provided, using DECIMAL for monetary values

FOREIGN KEY (customer_id) REFERENCES Customer(customer_id) ON


DELETE CASCADE, -- Foreign key constraint with CASCADE delete to remove
orders when a customer is deleted

FOREIGN KEY (product_id) REFERENCES Product(product_id) ON DELETE


CASCADE -- Foreign key constraint with CASCADE delete to remove orders when
a product is deleted

);

CREATE TABLE Sales (

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

amount DECIMAL(10, 2) NOT NULL, -- NOT NULL constraint to ensure


amount is provided, using DECIMAL for monetary values

FOREIGN KEY (employee_id) REFERENCES Employee(employee_id) ON


DELETE CASCADE -- Foreign key constraint with CASCADE delete to remove
sales when an employee is deleted

);

CREATE TABLE Customer (

customer_id INT PRIMARY KEY, -- Primary key constraint to uniquely identify


each customer

customer_name VARCHAR(100) NOT NULL -- NOT NULL constraint to ensure


customer name is provided

);

Applying Queries to Sample Data

Now let’s apply the above-defined complex queries to this sample data.

1. Find Employees Who Have More Than One Manager

• Expected Result: No employees have more than one manager in the sample data.
2. Find Products That Have Never Been Ordered

• Expected Result: Products 105 (Keyboard) have never been ordered.


3. Get the Top 5 Customers by Total Sales
customer_id total_sales
202 1600
201 1800
203 1000
204 1000
205 300

4. Find Employees Who Have Never Made a Sale

• 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: Employee 4 (David) has no direct reports.


7. Find the Department With the Most Employees

• 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.

Practice More Queries Using:

• Change aggregate conditions (e.g., SUM, AVG).


• Add different JOIN types (LEFT, RIGHT, FULL OUTER).
• Introduce subqueries and nested queries.
• Utilize window functions (ROW_NUMBER, RANK).
• Apply more filtering conditions (e.g., WHERE clauses with multiple conditions).

Q1. Find Employees Who Have More Than One Manager


Description: Retrieve the names of employees who have more than one manager.

Relational Algebra:

EMP_MANAGERS = π_employee_id, manager_id(EmployeeManager)


EMP_MULTI_MANAGERS = σ_count(*)>1(γ_employee_id; count(manager_id)
(EMP_MANAGERS)) RESULT = π_employee_name(EMP_MULTI_MANAGERS ⨝ Employee)

SQL:

SELECT e.employee_name FROM Employee e JOIN EmployeeManager em ON e.employee_id =


em.employee_id GROUP BY em.employee_id HAVING COUNT(em.manager_id) > 1;

Q2. Find Products That Have Never Been Ordered

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:

SELECT product_name FROM Product WHERE product_id NOT IN (SELECT DISTINCT


product_id FROM Order);

Q3. Get the Top 5 Customers by Total Sales

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:

SELECT customer_id, SUM(amount) AS total_sales FROM Orders GROUP BY customer_id


ORDER BY total_sales DESC LIMIT 5;

Q4. Find Employees Who Have Never Made a Sale

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);

Q5. List Customers Who Have Purchased All Products

Description: Find customers who have purchased all available products.

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);

5. List Customers Who Have Purchased All Products

Description: Find customers who have purchased all available products.

RA:

CUSTOMER_PRODUCTS = π_customer_id, product_id(Order) ALL_PRODUCTS =


π_product_id(Product) RESULT = γ_customer_id; COUNT(product_id)
(CUSTOMER_PRODUCTS) ÷ COUNT(ALL_PRODUCTS)

SQL:

SELECT customer_id FROM Orders GROUP BY customer_id HAVING COUNT(DISTINCT


product_id) = (SELECT COUNT(*) FROM Product);

Q6. Find All Managers With No Direct Reports

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:

SELECT e.manager_name FROM Employee e LEFT JOIN EmployeeManager em ON


e.employee_id = em.manager_id WHERE em.employee_id IS NULL;

Q7. Find the Department With the Most Employees

Description: Identify the department that has the highest number of employees.

RA:

EMP_COUNT = γ_department_id; COUNT(employee_id)(Employee) MAX_EMP_COUNT =


τ_COUNT(employee_id)(EMP_COUNT) RESULT = π_department_id(MAX_EMP_COUNT)

SQL:

SELECT department_id FROM Employee GROUP BY department_id ORDER BY


COUNT(employee_id) DESC LIMIT 1;

Q8. List All Products Purchased by More Than 100 Customers

Description: Find all product names that have been purchased by more than 100 unique customers.

RA:

PRODUCT_CUSTOMERS = γ_product_id; COUNT(DISTINCT customer_id)(Order) RESULT =


σ_COUNT(customer_id)>100(PRODUCT_CUSTOMERS) ⨝ Product

SQL:

SELECT product_name FROM Orders GROUP BY product_id HAVING COUNT(DISTINCT


customer_id) > 100;

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:

MAX_PRICE = MAX(π_price(Product)) MAX_PRODUCT = σ_price=MAX_PRICE(Product)


EXCLUSIVE_CUSTOMERS = π_customer_id(Order ⨝ MAX_PRODUCT) ALL_CUSTOMERS
= π_customer_id(Order) RESULT = EXCLUSIVE_CUSTOMERS - ALL_CUSTOMERS

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;

Q10. List Employees Who Are Also Customers

Description: Find employees who are also listed as customers.

RA:

EMPLOYEE_CUSTOMERS = Employee ⋈ Customer RESULT = π_employee_id,


employee_name(EMPLOYEE_CUSTOMERS)

SQL:

SELECT e.employee_id, e.employee_name FROM Employee e JOIN Customer c ON


e.employee_id = c.customer_id;

You might also like