Created
Oct 31, 2025
Last Modified
5 months ago

SQL quick recap

SQL quick recap

1. Basic SELECT & Filtering

Practice Questions

  1. Write an SQL query to display all records from the Employee table.

  2. Fetch employee names and salaries from Employee table where salary > 50000.

  3. Get all customers who live in 'Delhi'.

  4. Display unique city names from the Customer table.

  5. Show products with price between 500 and 2000.


🔠 2. Functions (String, Date, Math)

  1. Display employee names in uppercase.

  2. Extract the first 3 characters of employee name.

  3. Count number of employees who joined in year 2023.

  4. Display current date and time using SQL.

  5. Round salary to nearest thousand.


🔎 3. Sorting, WHERE, LIKE, IN, BETWEEN

  1. Fetch students whose names start with 'A'.

  2. Fetch employees whose department is in ('HR','IT','Finance').

  3. Find employees with joining date between '2023-01-01' and '2023-12-31'.

  4. Sort product list by price (highest first).

  5. Show customers whose phone number ends with '55'.


📊 4. GROUP BY + HAVING

  1. Count number of employees in each department.

  2. Display departments where average salary > 60000.

  3. Find total sales per customer.

  4. Show number of orders placed each month in 2024.

  5. List products sold more than 50 times.


🔗 5. JOINS (Very Important for Accenture)

Tables: Employee, Department, Orders, Customers

  1. Show employee name with their department name. (INNER JOIN)

  2. List all employees and departments even if dept doesn’t have employees. (RIGHT JOIN)

  3. Fetch customers and their orders. (LEFT JOIN)

  4. Show employees who have NO manager. (SELF JOIN)

  5. Display customer name and the total order value using JOIN + GROUP BY.


🧠 6. Subqueries

  1. Find employees whose salary is greater than the average salary.

  2. Display customers who have never placed an order.

  3. List employees who earn the second highest salary. (Use subquery)

  4. Fetch product details with price greater than the minimum price in table.

  5. Show departments where employee count > company avg employee count.


🧱 7. DDL (CREATE, ALTER, DROP)

  1. Create a table Student with fields: id, name, city, marks.

  2. Add a column email to the Student table.

  3. Modify datatype of salary column to decimal(10,2).

  4. Drop the department table.

  5. Rename table Employee to Staff.


✍️ 8. DML (INSERT, UPDATE, DELETE)

  1. Insert 3 rows into the Student table.

  2. Update city of student with id=5 to 'Mumbai'.

  3. Delete employees whose salary < 20000.

  4. Increase all salaries by 10%.

  5. Insert data from one table into another.


🧩 9. Constraints

  1. Create a table with PRIMARY KEY and NOT NULL constraints.

  2. Add UNIQUE constraint on employee email.

  3. Add CHECK constraint: salary must be > 10000.

  4. Add FOREIGN KEY referencing Department table.

  5. Remove a constraint.


🏎️ 10. Performance SQL – Important

  1. Difference between WHERE and HAVING – show example.

  2. Query with EXISTS vs IN – return employees with projects.

  3. Write query using DISTINCT with GROUP BY.

  4. Use LIMIT / TOP to fetch first 5 highest salaries.

  5. Use CASE to assign grade by marks.


📂 11. Views

  1. Create a view that stores employee name and dept.

  2. Create a view to show only employees with salary > 50000.

  3. Update data using view.

  4. Drop a view.

  5. Create a view with JOIN inside.


🔁 12. Stored Procedures

  1. Write a stored procedure to fetch all employees.

  2. Write a stored procedure that takes dept name as input and returns employee list.

  3. Procedure to increase salary by 5% for a dept.

  4. Procedure to count all orders of a customer.

  5. Delete a stored procedure.


13. Triggers (Accenture asks 1–2 questions)

  1. Create a trigger that logs deleted employee data.

  2. Trigger on update of salary – store old and new salary.

  3. Prevent insert if marks < 35.

  4. After insert into Orders, update stock table.

  5. Before delete on department, check if employees exist.


🧮 14. Advanced Queries

  1. Get 3rd highest salary without using MAX/ORDER BY.

  2. Find duplicate emails in user table.

  3. Delete duplicate rows, keep only 1.

  4. Show employees hired in last 7 days.

  5. Running total of sales (Window function).


SQL Solutions for all these questions


🗃️ Sample Table Structures (Assumed)

sql
-- Employee (emp_id, name, salary, dept_id, manager_id, joining_date)
-- Department (dept_id, dept_name)
-- Customer (cust_id, name, city, phone)
-- Orders (order_id, cust_id, product_id, order_date, amount)
-- Product (product_id, name, price, stock)
-- Student (id, name, city, marks)

✅ 1. Basic SELECT & Filtering

sql
-- Display all records from Employee table
SELECT * FROM Employee;

-- Fetch employee names and salaries where salary > 50000
SELECT name, salary FROM Employee WHERE salary > 50000;

-- Get all customers who live in 'Delhi'
SELECT * FROM Customer WHERE city = 'Delhi';

-- Display unique city names from Customer table
SELECT DISTINCT city FROM Customer;

-- Show products with price between 500 and 2000
SELECT * FROM Product WHERE price BETWEEN 500 AND 2000;

🔠 2. Functions (String, Date, Math)

sql
-- Display employee names in uppercase
SELECT UPPER(name) FROM Employee;

-- Extract first 3 characters of employee name
SELECT SUBSTRING(name, 1, 3) FROM Employee;

-- Count employees who joined in 2023
SELECT COUNT(*) FROM Employee 
WHERE YEAR(joining_date) = 2023;

-- Display current date and time
SELECT GETDATE(); -- SQL Server
-- SELECT NOW(); -- MySQL
-- SELECT SYSDATE FROM DUAL; -- Oracle

-- Round salary to nearest thousand
SELECT name, ROUND(salary, -3) AS rounded_salary FROM Employee;

🔎 3. Sorting, WHERE, LIKE, IN, BETWEEN

sql
-- Fetch students whose names start with 'A'
SELECT * FROM Student WHERE name LIKE 'A%';

-- Fetch employees in specific departments
SELECT * FROM Employee 
WHERE department IN ('HR', 'IT', 'Finance');

-- Find employees with joining date in 2023
SELECT * FROM Employee 
WHERE joining_date BETWEEN '2023-01-01' AND '2023-12-31';

-- Sort product list by price (highest first)
SELECT * FROM Product ORDER BY price DESC;

-- Show customers whose phone number ends with '55'
SELECT * FROM Customer WHERE phone LIKE '%55';

📊 4. GROUP BY + HAVING

sql
-- Count employees in each department
SELECT department, COUNT(*) as employee_count 
FROM Employee 
GROUP BY department;

-- Departments where average salary > 60000
SELECT department, AVG(salary) as avg_salary
FROM Employee 
GROUP BY department 
HAVING AVG(salary) > 60000;

-- Total sales per customer
SELECT cust_id, SUM(amount) as total_sales
FROM Orders 
GROUP BY cust_id;

-- Orders placed each month in 2024
SELECT MONTH(order_date) as month, COUNT(*) as order_count
FROM Orders 
WHERE YEAR(order_date) = 2024
GROUP BY MONTH(order_date);

-- Products sold more than 50 times
SELECT product_id, COUNT(*) as times_sold
FROM Orders 
GROUP BY product_id 
HAVING COUNT(*) > 50;

🔗 5. JOINS

sql
-- Employee name with department name (INNER JOIN)
SELECT e.name, d.dept_name 
FROM Employee e 
INNER JOIN Department d 
ON e.dept_id = d.dept_id;

-- All departments even if no employees (RIGHT JOIN)
SELECT d.dept_name, e.name 
FROM Employee e 
RIGHT JOIN Department d 
ON e.dept_id = d.dept_id;

-- Customers and their orders (LEFT JOIN)
SELECT c.name, o.order_id, o.order_date 
FROM Customer c 
LEFT JOIN Orders o 
ON c.cust_id = o.cust_id;

-- Employees who have NO manager (SELF JOIN)
SELECT e1.name 
FROM Employee e1 
LEFT JOIN Employee e2 
ON e1.manager_id = e2.emp_id 
WHERE e1.manager_id IS NULL;

-- Customer name and total order value
SELECT c.name, SUM(o.amount) as total_value
FROM Customer c 
JOIN Orders o 
ON c.cust_id = o.cust_id
GROUP BY c.name;

🧠 6. Subqueries

sql
-- Employees with salary > average salary
SELECT * FROM Employee 
WHERE salary > (SELECT AVG(salary) FROM Employee);

-- Customers who never placed an order
SELECT * FROM Customer 
WHERE cust_id NOT IN (SELECT DISTINCT cust_id FROM Orders);

-- Employees with second highest salary
SELECT * FROM Employee 
WHERE salary = (
    SELECT MAX(salary) FROM Employee 
    WHERE salary < (SELECT MAX(salary) FROM Employee)
);

-- Products with price > minimum price
SELECT * FROM Product 
WHERE price > (SELECT MIN(price) FROM Product);

-- Departments where employee count > company average
SELECT department, COUNT(*) as emp_count
FROM Employee 
GROUP BY department
HAVING COUNT(*) > (
    SELECT AVG(emp_count) FROM (
        SELECT COUNT(*) as emp_count 
        FROM Employee 
        GROUP BY department
    ) dept_counts
);

🧱 7. DDL (CREATE, ALTER, DROP)

sql
-- Create Student table
CREATE TABLE Student (
    id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    city VARCHAR(50),
    marks DECIMAL(5,2)
);

-- Add email column
ALTER TABLE Student ADD email VARCHAR(100);

-- Modify salary datatype
ALTER TABLE Employee ALTER COLUMN salary DECIMAL(10,2);

-- Drop department table
DROP TABLE Department;

-- Rename Employee to Staff
EXEC sp_rename 'Employee', 'Staff'; -- SQL Server
-- ALTER TABLE Employee RENAME TO Staff; -- MySQL

✍️ 8. DML (INSERT, UPDATE, DELETE)

sql
-- Insert 3 rows into Student
INSERT INTO Student (id, name, city, marks) VALUES
(1, 'John Doe', 'Mumbai', 85.5),
(2, 'Jane Smith', 'Delhi', 92.0),
(3, 'Bob Wilson', 'Chennai', 78.5);

-- Update city of student with id=5
UPDATE Student SET city = 'Mumbai' WHERE id = 5;

-- Delete employees with salary < 20000
DELETE FROM Employee WHERE salary < 20000;

-- Increase all salaries by 10%
UPDATE Employee SET salary = salary * 1.10;

-- Insert data from one table to another
INSERT INTO Employee_Backup 
SELECT * FROM Employee WHERE dept_id = 1;

🧩 9. Constraints

sql
-- Create table with constraints
CREATE TABLE Employee (
    emp_id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100) UNIQUE,
    salary DECIMAL(10,2) CHECK (salary > 10000),
    dept_id INT,
    FOREIGN KEY (dept_id) REFERENCES Department(dept_id)
);

-- Add UNIQUE constraint
ALTER TABLE Employee 
ADD CONSTRAINT UQ_Employee_Email UNIQUE (email);

-- Add CHECK constraint
ALTER TABLE Employee 
ADD CONSTRAINT CHK_Salary CHECK (salary > 10000);

-- Add FOREIGN KEY
ALTER TABLE Employee 
ADD CONSTRAINT FK_Employee_Department 
FOREIGN KEY (dept_id) REFERENCES Department(dept_id);

-- Remove constraint
ALTER TABLE Employee 
DROP CONSTRAINT CHK_Salary;

🏎️ 10. Performance SQL

sql
-- WHERE vs HAVING example
SELECT department, AVG(salary) as avg_salary
FROM Employee 
WHERE joining_date > '2023-01-01'  -- Filters rows before grouping
GROUP BY department 
HAVING AVG(salary) > 50000;        -- Filters after grouping

-- EXISTS vs IN
SELECT name FROM Employee e
WHERE EXISTS (
    SELECT 1 FROM Projects p 
    WHERE p.emp_id = e.emp_id
);

-- DISTINCT with GROUP BY
SELECT DISTINCT department, COUNT(*) 
FROM Employee 
GROUP BY department;

-- First 5 highest salaries
SELECT TOP 5 salary FROM Employee ORDER BY salary DESC; -- SQL Server
-- SELECT salary FROM Employee ORDER BY salary DESC LIMIT 5; -- MySQL

-- CASE for grade assignment
SELECT name, marks,
    CASE 
        WHEN marks >= 90 THEN 'A'
        WHEN marks >= 80 THEN 'B' 
        WHEN marks >= 70 THEN 'C'
        ELSE 'D'
    END as grade
FROM Student;

📂 11. Views

sql
-- Create view with employee name and department
CREATE VIEW EmployeeDeptView AS
SELECT e.name, d.dept_name 
FROM Employee e 
JOIN Department d ON e.dept_id = d.dept_id;

-- View for employees with salary > 50000
CREATE VIEW HighSalaryEmployees AS
SELECT * FROM Employee WHERE salary > 50000;

-- Update data using view (if updatable)
UPDATE HighSalaryEmployees SET salary = 60000 WHERE emp_id = 101;

-- Drop view
DROP VIEW EmployeeDeptView;

-- View with JOIN
CREATE VIEW CustomerOrderView AS
SELECT c.name, o.order_date, o.amount 
FROM Customer c 
JOIN Orders o ON c.cust_id = o.cust_id;

🔁 12. Stored Procedures

sql
-- Fetch all employees
CREATE PROCEDURE GetAllEmployees
AS
BEGIN
    SELECT * FROM Employee;
END;

-- Get employees by department
CREATE PROCEDURE GetEmployeesByDept
    @DeptName VARCHAR(50)
AS
BEGIN
    SELECT e.* 
    FROM Employee e 
    JOIN Department d ON e.dept_id = d.dept_id
    WHERE d.dept_name = @DeptName;
END;

-- Increase salary for department
CREATE PROCEDURE IncreaseSalaryByDept
    @DeptId INT,
    @Percent DECIMAL(5,2)
AS
BEGIN
    UPDATE Employee 
    SET salary = salary * (1 + @Percent/100)
    WHERE dept_id = @DeptId;
END;

-- Count orders for customer
CREATE PROCEDURE CountCustomerOrders
    @CustId INT,
    @OrderCount INT OUTPUT
AS
BEGIN
    SELECT @OrderCount = COUNT(*) 
    FROM Orders 
    WHERE cust_id = @CustId;
END;

-- Delete stored procedure
DROP PROCEDURE GetAllEmployees;

⚡ 13. Triggers

sql
-- Log deleted employee data
CREATE TRIGGER LogDeletedEmployee
ON Employee
AFTER DELETE
AS
BEGIN
    INSERT INTO Employee_Archive 
    SELECT *, GETDATE() FROM deleted;
END;

-- Track salary changes
CREATE TRIGGER TrackSalaryChange
ON Employee
AFTER UPDATE
AS
BEGIN
    IF UPDATE(salary)
    BEGIN
        INSERT INTO Salary_History 
        SELECT i.emp_id, d.salary, i.salary, GETDATE()
        FROM inserted i JOIN deleted d ON i.emp_id = d.emp_id;
    END
END;

-- Prevent insert if marks < 35
CREATE TRIGGER PreventLowMarks
ON Student
INSTEAD OF INSERT
AS
BEGIN
    IF (SELECT marks FROM inserted) >= 35
    BEGIN
        INSERT INTO Student SELECT * FROM inserted;
    END
    ELSE
    BEGIN
        PRINT 'Marks cannot be less than 35';
    END
END;

-- Update stock after order
CREATE TRIGGER UpdateStockAfterOrder
ON Orders
AFTER INSERT
AS
BEGIN
    UPDATE Product 
    SET stock = stock - 1 
    WHERE product_id IN (SELECT product_id FROM inserted);
END;

-- Check employees before department delete
CREATE TRIGGER PreventDeptDelete
ON Department
INSTEAD OF DELETE
AS
BEGIN
    IF NOT EXISTS (SELECT 1 FROM Employee WHERE dept_id IN (SELECT dept_id FROM deleted))
    BEGIN
        DELETE FROM Department WHERE dept_id IN (SELECT dept_id FROM deleted);
    END
    ELSE
    BEGIN
        PRINT 'Cannot delete department with existing employees';
    END
END;

🧮 14. Advanced Queries

sql
-- 3rd highest salary without MAX/ORDER BY
SELECT salary FROM Employee e1
WHERE 2 = (SELECT COUNT(DISTINCT salary) 
           FROM Employee e2 
           WHERE e2.salary > e1.salary);

-- Find duplicate emails
SELECT email, COUNT(*) 
FROM User 
GROUP BY email 
HAVING COUNT(*) > 1;

-- Delete duplicates, keep one
WITH CTE AS (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY email ORDER BY id) as rn
    FROM User
)
DELETE FROM CTE WHERE rn > 1;

-- Employees hired in last 7 days
SELECT * FROM Employee 
WHERE joining_date >= DATEADD(day, -7, GETDATE());

-- Running total of sales
SELECT order_date, amount,
       SUM(amount) OVER (ORDER BY order_date) as running_total
FROM Orders;