SQL Practice
Key SQL Topics to Focus On:
1. Data Definition Language (DDL):
○ Creating, altering, and dropping tables.
○ Understanding data types and constraints.
2. Data Manipulation Language (DML):
○ Inserting, updating, and deleting records.
○ Querying data using the SELECT statement.
3. SQL Operators:
○ Arithmetic, comparison, and logical operators.
4. Joins:
○ Inner, left, right, and full joins.
○ Self-joins and cross joins.
5. Subqueries:
○ Nested queries and correlated subqueries.
6. Aggregate Functions:
○ COUNT, SUM, AVG, MAX, MIN.
○ Grouping data with GROUP BY and filtering groups with HAVING.
7. Set Operations:
○ UNION, INTERSECT, and EXCEPT.
8. Indexes and Views:
○ Creating and managing indexes.
○ Defining and using views.
SQL Practice Tables, Schemas, and Sample Data
1. Employees Table
Schema:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(50),
salary INT,
manager_id INT,
join_date DATE);
Sample Data:
INSERT INTO employees (id, name, department, salary, manager_id,
join_date) VALUES
(1, 'Alice', 'HR', 60000, NULL, '2020-05-12'),
(2, 'Bob', 'IT', 80000, 1, '2019-03-25'),
(3, 'Charlie', 'IT', 75000, 1, '2021-07-19'),
(4, 'David', 'Finance', 90000, 2, '2018-06-10'),
(5, 'Eve', 'Finance', 95000, 2, '2017-08-03'),
(6, 'Frank', 'HR', 65000, 1, '2022-09-15'),
(7, 'Grace', 'IT', 85000, 3, '2020-11-05'),
(8, 'Hank', 'Finance', 72000, 4, '2021-02-20'),
(9, 'Ivy', 'HR', 62000, 6, '2019-04-30'),
(10, 'Jack', 'IT', 78000, 3, '2022-01-10');
2. Customers Table
Schema:
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
name VARCHAR(100),
city VARCHAR(50)
);
Sample Data:
INSERT INTO customers (customer_id, name, city) VALUES
(1, 'John Doe', 'New York'),
(2, 'Jane Smith', 'Los Angeles'),
(3, 'Robert Brown', 'Chicago');
3. Orders Table
Schema:
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
amount DECIMAL(10,2),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
Sample Data:
INSERT INTO orders (order_id, customer_id, order_date, amount) VALUES
(101, 1, '2024-01-10', 150.50),
(102, 1, '2024-02-15', 200.00),
(103, 2, '2024-03-05', 350.75),
(104, 3, '2024-01-20', 500.00);
4. Order Details Table
Schema:
CREATE TABLE order_details (
order_detail_id INT PRIMARY KEY,
order_id INT,
product_id INT,
quantity INT,
FOREIGN KEY (order_id) REFERENCES orders(order_id)
);
Sample Data:
INSERT INTO order_details (order_detail_id, order_id, product_id, quantity) VALUES
(1, 101, 1, 2),
(2, 102, 2, 1),
(3, 103, 3, 5),
(4, 104, 4, 3);
5. Products Table
Schema:
CREATE TABLE products (
product_id INT PRIMARY KEY,
name VARCHAR(100),
price DECIMAL(10,2)
);
Sample Data:
INSERT INTO products (product_id, name, price) VALUES
(1, 'Laptop', 1000.00),
(2, 'Phone', 500.00),
(3, 'Tablet', 300.00),
(4, 'Monitor', 200.00);
SQL Practice Questions and Solutions
1. Basic SQL Queries
Q1: Retrieve all employee details
Query:
SELECT * FROM employees;
Q2: Retrieve names and salaries of all employees in the IT department
Query:
SELECT name, salary FROM employees WHERE department = 'IT';
Q3: Retrieve employees who joined before 2020
Query:
SELECT * FROM employees WHERE join_date < '2020-01-01';
2. Aggregate Functions
Q4: Find the total salary paid to employees in the HR department
Query:
SELECT SUM(salary) FROM employees WHERE department = 'HR';
Q5: Find the average salary in each department
Query:
SELECT department, AVG(salary) FROM employees GROUP BY department;
Q6: Find the highest salary in the company
Query:
SELECT MAX(salary) FROM employees;
3. Joins
Q7: Retrieve all orders with customer names
Query:
SELECT orders.order_id, [Link], [Link] FROM orders
JOIN customers ON orders.customer_id = customers.customer_id;
Q8: Retrieve employees and their managers' names
Query:
SELECT [Link] AS Employee, [Link] AS Manager FROM employees e
LEFT JOIN employees m ON e.manager_id = [Link];
4. Subqueries
Q9: Find employees earning more than the average salary
Query:
SELECT name, salary FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
Q10: Find customers who have placed orders
Query:
SELECT name FROM customers WHERE customer_id IN (SELECT customer_id FROM
orders);
5. Window Functions
Q11: Rank employees by salary within each department
Query:
SELECT name, department, salary,
RANK() OVER(PARTITION BY department ORDER BY salary DESC) AS Rank
FROM employees;
Q12: Find the cumulative sum of salaries ordered by join date
Query:
SELECT name, salary, join_date,
SUM(salary) OVER(ORDER BY join_date) AS Cumulative_Salary
FROM employees;
6. String Functions
Q13: Convert all employee names to uppercase
Query:
SELECT UPPER(name) FROM employees;
Q14: Extract the first three characters of employee names
Query:
SELECT LEFT(name, 3) FROM employees;
7. Date Functions
Q15: Find employees who joined in the last 2 years
Query:
SELECT * FROM employees WHERE join_date >= DATE_SUB(CURDATE(), INTERVAL 2
YEAR);
Q16: Extract the year of joining for each employee
Query:
SELECT name, YEAR(join_date) AS Join_Year FROM employees;
8. Case Statements
Q17: Categorize employees based on salary range
Query:
SELECT name, salary,
CASE
WHEN salary < 70000 THEN 'Low'
WHEN salary BETWEEN 70000 AND 90000 THEN 'Medium'
ELSE 'High'
END AS Salary_Category
FROM employees;
Q18: Display a message for employees based on their department
Query:
SELECT name, department,
CASE department
WHEN 'IT' THEN 'Tech Team'
WHEN 'HR' THEN 'People Management'
ELSE 'Finance & Others'
END AS Department_Info
FROM employees;
9. Set Operations
Q19: Find employees who are also customers
Query:
SELECT name FROM employees
INTERSECT
SELECT name FROM customers;
Q20: Find names that exist in employees but not in customers
Query:
SELECT name FROM employees
EXCEPT
SELECT name FROM customers;
10. Advanced Queries
Q21: Retrieve the second highest salary in the company
Query:
SELECT DISTINCT salary FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
Q22: Retrieve departments where more than 2 employees work
Query:
SELECT department FROM employees
GROUP BY department
HAVING COUNT(*) > 2;
Q23: Find employees who don’t have a manager
Query:
SELECT name FROM employees WHERE manager_id IS NULL;
Q24: Find the most recently joined employee in each department
Query:
SELECT name, department, join_date
FROM (
SELECT name, department, join_date,
RANK() OVER(PARTITION BY department ORDER BY join_date DESC) AS rnk
FROM employees
) temp WHERE rnk = 1;
Q25: Find the top 3 highest-paid employees
Query:
SELECT name, salary FROM employees ORDER BY salary DESC LIMIT 3;
Q26: Count the number of orders placed by each customer
Query:
SELECT customer_id, COUNT(*) AS Order_Count FROM orders
GROUP BY customer_id;
Q27: Find customers who have never placed an order
Query:
SELECT name FROM customers WHERE customer_id NOT IN (SELECT DISTINCT
customer_id FROM orders);
Q28: Find the employee with the longest tenure
Query:
SELECT name, join_date FROM employees ORDER BY join_date ASC LIMIT 1;
Q29: Find duplicate salaries in the employees table
Query:
SELECT salary, COUNT(*) FROM employees GROUP BY salary HAVING COUNT(*) > 1;
Q30: Retrieve products that have never been ordered
Query:
SELECT name FROM products WHERE product_id NOT IN (SELECT DISTINCT product_id
FROM order_details);