Basic Level Questions
SQL Practice Exercises
SELECT Queries
- Select all columns from the employees table.
- Select only the first_name and last_name from customers.
- Select unique department values from employees.
- Select all orders placed in 2024.
- Select the top 10 customers by customer_id.
WHERE Clause
- Select users older than 30 years.
- Find products with price less than 100.
- Get employees in the "Marketing" department.
- Select customers from 'New York'.
- Find orders placed between '2023-01-01' and '2023-06-30'.
ORDER BY
- Get all products ordered by price descending.
- List employees ordered by join date.
- Show customers alphabetically.
- Order orders by order amount, highest first.
- List top 5 highest-paid employees.
LIMIT/TOP
- Get the first 5 records from employees.
- Show the last 10 orders placed.
- Show top 3 products by rating.
- Limit the number of customers to 100.
- Get any 1 random row from products.
Intermediate Level
INSERT, UPDATE, DELETE
- Insert a new product.
- Update the salary of an employee.
- Delete a customer by ID.
- Insert 3 records into the departments table.
- Increase price of all products by 10%.
AGGREGATE FUNCTIONS
- Count total number of employees.
- Find the average product price.
- Get the max and min salary.
- Count how many orders a user has made.
- Find total sales for April.
GROUP BY
- Group employees by department.
- Get total number of customers per city.
- Show average price per product category.
- Find number of orders per day.
- Count products grouped by brand.
HAVING
- Get departments with more than 5 employees.
- Show cities with more than 10 customers.
- Find products with average rating above 4.
- Count employees in departments with over 3 managers.
- Filter categories with more than 20 products.
JOINS
- Get employee names with their department names.
- Join orders and customers to show order details.
- List all products with their categories.
- Show comments with user info (post + user join).
- Show all students and their attendance records.
Advanced Level
Subqueries
- Get employees earning more than the average salary.
- List customers who made more than 5 orders.
- Find users who haven't placed any orders.
- Get the latest order for each customer.
- Select the highest-paid employee in each department.
CASE Statements
- Classify employees as 'Junior', 'Mid', 'Senior' based on salary.
- Show customer status based on total order amount.
- Label products as 'Cheap', 'Moderate', or 'Expensive'.
- Determine if students passed or failed based on marks.
- Use CASE to calculate bonus for employees.
String Functions
- Get the first 5 characters of a product name.
- Convert customer names to uppercase.
- Concatenate first and last name into full name.
- Replace 'Inc' with 'Ltd' in company names.
- Count length of product descriptions.
Date Functions
- Get today's orders.
- Extract month from order_date.
- Find users who joined last year.
- List events happening this week.
- Show age of users using birthdate.
Window Functions (Advanced SQL)
- Rank products by rating within category.
- Show running total of sales by day.
- Use LAG to get previous day's order count.
- Use LEAD to show next salary value.
- Find highest-paid employee in each department using RANK.
Schema-Based Challenges
Students System
- Get average marks per student.
- Show students who failed any subject.
- Find top 3 scorers in each class.
- List classes with no students.
- Rank students in class 10 by average score.
Blogging Platform
- List all posts with author names.
- Count comments per post.
- Get top 5 most commented posts.
- Show posts with no comments.
- Find users who posted in the last 7 days.
E-Commerce Store
- Show all orders with customer and product info.
- Find best-selling product.
- Get total sales per category.
- List customers who placed multiple orders.
- Find products that were never sold.
Social Media
- Count likes per post.
- Find most followed user.
- Show posts from people the user follows.
- List users who haven't posted anything.
- Show recent activity in the last 24 hours.
Bonus: Practice Questions for Constraints and Admin
Constraints & Permissions
- Create a table with PRIMARY KEY and NOT NULL.
- Add a FOREIGN KEY constraint.
- Update a table to add a UNIQUE constraint.
- Revoke permissions from a user.
- Grant SELECT and INSERT to a role.
Views, Indexes, Procedures
- Create a view to show active employees.
- Write a stored procedure to calculate tax.
- Create an index on the order_date column.
- Use a trigger to prevent deleting a VIP user.
- Create a materialized view for monthly sales.