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

Basic SQL Interview Questions

SQL Interview Questions shjsjxjbdbdjdkkdkdknsbsbsjjsjs

Uploaded by

abbi abbi
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)
25 views

Basic SQL Interview Questions

SQL Interview Questions shjsjxjbdbdjdkkdkdknsbsbsjjsjs

Uploaded by

abbi abbi
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/ 17

SQL QUESTION SERIES (CREATE INSERT SYNTAX OF DATASET ARE ALSO GIVEN)

– PRAGYA RATHI

BASIC

Question: Retrieve all employees with a salary greater than 5000.

CREATE TABLE employees (


emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
department VARCHAR(50),
salary INT
);

INSERT INTO employees (emp_id, emp_name, department, salary) VALUES


(1, 'John', 'HR', 4500),
(2, 'Jane', 'Finance', 6000),
(3, 'Mike', 'IT', 5500);

SELECT *
FROM employees
WHERE salary > 5000;

Question: List all employees who joined within the last 12 months.

-- Create table
CREATE TABLE employee_details (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
joining_date DATE
);

-- Insert data
INSERT INTO employee_details (emp_id, emp_name, joining_date) VALUES
(1, 'John', '2023-10-01'),
(2, 'Jane', '2022-12-15'),
(3, 'Mike', '2024-01-10'),
(4, 'Emily', '2023-09-20'),
(5, 'Alex', '2022-07-30');
-- Query
SELECT *
FROM employee_details
WHERE joining_date >= CURRENT_DATE - INTERVAL '1 year';

Retrieve Employees Who Work in a IT Department

-- Query
SELECT *
FROM employees
WHERE department = 'IT';

Retrieve employees whose names contain the letter 'a'.

-- Query
SELECT * FROM employees WHERE emp_name LIKE '%a%';

List All Products with Price Between100 and 500

-- Create table
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(50),
price DECIMAL(10, 2)
);

-- Insert data
INSERT INTO products (product_id, product_name, price) VALUES
(1, 'Laptop', 800.00),
(2, 'Smartphone', 300.00),
(3, 'Tablet', 150.00),
(4, 'Headphones', 50.00),
(5, 'Monitor', 250.00);
-- Query
SELECT * FROM products WHERE price BETWEEN 100 AND 500;

Count the total number of employees in each department.

-- Query
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;

Find the Product with the Maximum Price

-- Query
SELECT * FROM products ORDER BY price DESC LIMIT 1;

Find the total salary expense for each department.

-- Query
SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department;

Retrieve employees whose salary is below the company average.

-- Query
SELECT * FROM employees WHERE salary < (SELECT AVG(salary) FROM employees);

Get all orders placed by the customer 'John'.


-- Create table
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_name VARCHAR(50),
total_amount DECIMAL(10, 2)
);

-- Insert data
INSERT INTO orders (order_id, customer_name, total_amount) VALUES
(1, 'John', 150.00),
(2, 'Anna', 250.50),
(3, 'John', 175.75),
(4, 'Laura', 120.00);

-- Query
SELECT * FROM orders WHERE customer_name = 'John';

List customers who haven't placed any orders yet.

-- Create table
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(50)
);

-- Insert data
INSERT INTO customers (customer_id, customer_name) VALUES
(1, 'John'),
(2, 'Anna'),
(3, 'Sam'),
(4, 'Laura');

-- Query
SELECT c.customer_name
FROM customers c
LEFT JOIN orders o ON c.customer_name = o.customer_name
WHERE o.order_id IS NULL;
Retrieve orders placed within the last 30 days.

-- Insert data (continuation from previous example)


INSERT INTO orders (order_id, customer_name, total_amount) VALUES
(5, 'Jack', 300.20);

-- Query
SELECT * FROM orders WHERE order_id IN
(SELECT order_id FROM orders WHERE order_date >= CURRENT_DATE - INTERVAL '30
DAY');

List customers who have placed more than one order.

-- Query
SELECT customer_name, COUNT(*) AS order_count
FROM orders
GROUP BY customer_name
HAVING COUNT(*) > 1;

Get the date when the maximum number of orders were placed.

-- Query
SELECT order_date, COUNT(*) AS order_count
FROM orders
GROUP BY order_date
ORDER BY order_count DESC LIMIT 1;

Get all products sold in quantities greater than 100.

-- Create table
CREATE TABLE sales (
sale_id INT PRIMARY KEY,
product_id INT,
quantity_sold INT,
FOREIGN KEY (product_id) REFERENCES products(product_id)
);

-- Insert data
INSERT INTO sales (sale_id, product_id, quantity_sold) VALUES
(1, 1, 50),
(2, 2, 150),
(3, 3, 200),
(4, 1, 75),
(5, 2, 80);

-- Query
SELECT p.product_name, SUM(s.quantity_sold) AS total_sold
FROM sales s
JOIN products p ON s.product_id = p.product_id
GROUP BY p.product_name
HAVING total_sold > 100;

Retrieve orders where the total value is greater than 500.

-- Query
SELECT * FROM orders WHERE total_amount > 500;

Calculate the average value of all orders.

-- Query
SELECT AVG(total_amount) AS average_order_value FROM orders;

List all employees hired before 2020.

-- Create table with hiring date


CREATE TABLE employee_hiring (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
hiring_date DATE
);

-- Insert data
INSERT INTO employee_hiring (emp_id, emp_name, hiring_date) VALUES
(1, 'John', '2019-06-15'),
(2, 'Jane', '2021-01-20'),
(3, 'Mike', '2018-11-30'),
(4, 'Emily', '2020-05-15');

-- Query
SELECT * FROM employee_hiring WHERE hiring_date < '2020-01-01';

Get a list of customers who purchased 'Laptop'.

-- Insert data for product purchases


CREATE TABLE product_purchases (
purchase_id INT PRIMARY KEY,
customer_name VARCHAR(50),
product_name VARCHAR(50)
);

-- Insert data
INSERT INTO product_purchases (purchase_id, customer_name, product_name)
VALUES
(1, 'John', 'Laptop'),
(2, 'Anna', 'Smartphone'),
(3, 'Mike', 'Laptop'),
(4, 'Laura', 'Tablet');

-- Query
SELECT DISTINCT customer_name
FROM product_purchases
WHERE product_name = 'Laptop';

Calculate the total sales amount for each product.


-- Query
SELECT p.product_name, SUM(s.quantity_sold) AS total_sales
FROM sales s
JOIN products p ON s.product_id = p.product_id
GROUP BY p.product_name;

Get all orders shipped to 'New York'.

-- Create table for shipping information


CREATE TABLE order_shipping (
order_id INT,
shipping_city VARCHAR(50)
);

-- Insert data
INSERT INTO order_shipping (order_id, shipping_city) VALUES
(1, 'New York'),
(2, 'Los Angeles'),
(3, 'New York'),
(4, 'Chicago');

-- Query
SELECT o.*
FROM orders o
JOIN order_shipping s ON o.order_id = s.order_id
WHERE s.shipping_city = 'New York';

List all products that have never been sold.

-- Query
SELECT p.product_name
FROM products p
LEFT JOIN sales s ON p.product_id = s.product_id
WHERE s.sale_id IS NULL;
Count the number of orders placed in each month.

-- Query
SELECT DATE_TRUNC('month', order_date) AS month, COUNT(*) AS total_orders
FROM orders
GROUP BY month;

List employees who have held more than one job title.

-- Create table for job history


CREATE TABLE employee_jobs (
emp_id INT,
job_title VARCHAR(50),
PRIMARY KEY (emp_id, job_title)
);

-- Insert data
INSERT INTO employee_jobs (emp_id, job_title) VALUES
(1, 'Manager'),
(1, 'Senior Manager'),
(2, 'Analyst'),
(3, 'Analyst'),
(3, 'Senior Analyst');

-- Query
SELECT emp_id, COUNT(DISTINCT job_title) AS job_count
FROM employee_jobs
GROUP BY emp_id
HAVING COUNT(DISTINCT job_title) > 1;

Retrieve the first order placed by each customer.

-- Query
SELECT customer_name, MIN(order_id) AS first_order_id
FROM orders
GROUP BY customer_name;
Find the percentage of total sales contributed by each product.

-- Query
SELECT p.product_name,
SUM(s.quantity_sold) * 100.0 / (SELECT SUM(quantity_sold) FROM sales) AS
sales_percentage
FROM sales s
JOIN products p ON s.product_id = p.product_id
GROUP BY p.product_name;

List customers with the highest total purchase amounts.

-- Query
SELECT customer_name, SUM(total_amount) AS total_purchase
FROM orders
GROUP BY customer_name
ORDER BY total_purchase DESC
LIMIT 1;

Retrieve the average salary for employees per year.

-- Create table with hiring year


CREATE TABLE employee_salaries (
emp_id INT,
salary INT,
hiring_year INT
);

-- Insert data
INSERT INTO employee_salaries (emp_id, salary, hiring_year) VALUES
(1, 4000, 2019),
(2, 5000, 2020),
(3, 6000, 2021),
(4, 7000, 2022);
-- Query
SELECT hiring_year, AVG(salary) AS average_salary
FROM employee_salaries
GROUP BY hiring_year;

Retrieve products with stock less than a certain threshold.

-- Create table with stock


CREATE TABLE product_stock (
product_id INT,
stock INT
);

-- Insert data
INSERT INTO product_stock (product_id, stock) VALUES
(1, 50),
(2, 20),
(3, 10),
(4, 0);

-- Query
SELECT p.product_name
FROM products p
JOIN product_stock ps ON p.product_id = ps.product_id
WHERE ps.stock < 20;

-- Create table with stock


CREATE TABLE product_stock (
product_id INT,
stock INT
);

-- Insert data
INSERT INTO product_stock (product_id, stock) VALUES
(1, 50),
(2, 20),
(3, 10),
(4, 0);
-- Query
SELECT p.product_name
FROM products p
JOIN product_stock ps ON p.product_id = ps.product_id
WHERE ps.stock < 20;

Find employees who share the same job title.

-- Query
SELECT job_title, GROUP_CONCAT(emp_name) AS employees
FROM employee_jobs
GROUP BY job_title
HAVING COUNT(emp_id) > 1;

List all employees hired after January 1, 2020.

-- Query
SELECT * FROM employee_hiring WHERE hiring_date > '2020-01-01';

Retrieve customers who have not made any purchases.

-- Query
SELECT c.customer_name
FROM customers c
LEFT JOIN product_purchases pp ON c.customer_name = pp.customer_name
WHERE pp.purchase_id IS NULL;

Count how many orders are in each status (e.g., shipped, pending).

-- Create table for order status


CREATE TABLE order_status (
order_id INT,
status VARCHAR(50)
);

-- Insert data
INSERT INTO order_status (order_id, status) VALUES
(1, 'Shipped'),
(2, 'Pending'),
(3, 'Shipped'),
(4, 'Pending');

-- Query
SELECT status, COUNT(*) AS order_count
FROM order_status
GROUP BY status;

Find employees who are within 5 years of retirement age (assumed to be 60).

-- Create table with employee age


CREATE TABLE employee_age (
emp_id INT,
emp_name VARCHAR(50),
age INT
);

-- Insert data
INSERT INTO employee_age (emp_id, emp_name, age) VALUES
(1, 'John', 55),
(2, 'Jane', 40),
(3, 'Mike', 59),
(4, 'Emily', 45);

-- Query
SELECT emp_name
FROM employee_age
WHERE age >= 55; -- Within 5 years of 60

Calculate total sales for each region.


-- Create table for sales region
CREATE TABLE sales_region (
sale_id INT,
region VARCHAR(50),
amount DECIMAL(10, 2)
);

-- Insert data
INSERT INTO sales_region (sale_id, region, amount) VALUES
(1, 'North', 150.00),
(2, 'South', 250.50),
(3, 'North', 175.75),
(4, 'East', 120.00);

-- Query
SELECT region, SUM(amount) AS total_sales
FROM sales_region
GROUP BY region;

Find the employee turnover rate for the last year.

-- Create table for employee status


CREATE TABLE employee_status (
emp_id INT,
status VARCHAR(50),
termination_date DATE
);

-- Insert data
INSERT INTO employee_status (emp_id, status, termination_date) VALUES
(1, 'Active', NULL),
(2, 'Terminated', '2023-08-15'),
(3, 'Active', NULL),
(4, 'Terminated', '2024-02-01');

-- Query
SELECT (SELECT COUNT(*) FROM employee_status WHERE status = 'Terminated' AND
termination_date >= CURRENT_DATE - INTERVAL '1 year')::DECIMAL /
(SELECT COUNT(*) FROM employee_status) AS turnover_rate;
Calculate the average order value for each customer.

-- Query
SELECT customer_name, AVG(total_amount) AS average_order_value
FROM orders
GROUP BY customer_name;

Find products that have a return rate greater than 10%.

-- Create table for returns


CREATE TABLE product_returns (
return_id INT,
product_id INT,
returned_quantity INT
);

-- Insert data
INSERT INTO product_returns (return_id, product_id, returned_quantity) VALUES
(1, 1, 5),
(2, 2, 15),
(3, 2, 2),
(4, 3, 1);

-- Query
SELECT p.product_name,
SUM(pr.returned_quantity) * 100.0 / SUM(s.quantity_sold) AS return_rate
FROM products p
JOIN product_returns pr ON p.product_id = pr.product_id
JOIN sales s ON p.product_id = s.product_id
GROUP BY p.product_name
HAVING return_rate > 10;

List products with discounts greater than 20%.

-- Create table for discounts


CREATE TABLE product_discounts (
product_id INT,
discount_percentage DECIMAL(5, 2)
);

-- Insert data
INSERT INTO product_discounts (product_id, discount_percentage) VALUES
(1, 10.00),
(2, 25.00),
(3, 30.00),
(4, 15.00);

-- Query
SELECT p.product_name
FROM products p
JOIN product_discounts d ON p.product_id = d.product_id
WHERE d.discount_percentage > 20;

List the top 5 products sold by quantity.

-- Query
SELECT p.product_name, SUM(s.quantity_sold) AS total_quantity_sold
FROM sales s
JOIN products p ON s.product_id = p.product_id
GROUP BY p.product_name
ORDER BY total_quantity_sold DESC
LIMIT 5;

You might also like