SQL Que
SQL Que
SQL Que
General instructions:
This document is created by keeping in mind that student can do more and
more practice in SQL concepts. So, please first install MySQL in your
laptop/computer.
◼ Create a Database.
◼ Then create the tables given in questions.
◼ Insert data given in questions.
◼ Read questions and try to write Query on your own.
◼ If needed, then only see the answer.
Author
Ritesh Kumar Sinha
https://www.careerswitchhelp.com
Follow on Instagram:
https://www.instagram.com/sql_interview_question
1
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Table of Contents
• Practice Set 1 – Online Bookstore ……………………... 10 Question
• Practice Set 2 – Employee Management system …….... 10 Question
• Practice Set 3 – Retail store …………………….............. 20 Question
• Aggregate function ……………………………………… 20 Question
• Analytical Function ……………………………................. 20 Question
• Joins and subqueries ……………………………………. 20 Question
• STRING Functions ……………………………………….. 20 Question
• CASE Statements ………………………………………... 20 Question
• REGEX (Regular Expressions) …………………………… 20 Question
• DATE Function …………………………………………… 20 Question
• Numeric Functions ……………………………………….. 20 Question
2
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Here's a case study with questions and answers that can be solved using
MySQL:
Consider an online bookstore that sells books to customers. The bookstore
maintains a database with the following tables:
Table: books
Columns:
- book_id (int, primary key)
- title (varchar)
- author (varchar)
- price (decimal)
Table: customers
Columns:
- customer_id (int, primary key)
- name (varchar)
- email (varchar)
- address (varchar)
3
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Table: orders
Columns:
- order_id (int, primary key)
- customer_id (int, foreign key referencing customers.customer_id)
- book_id (int, foreign key referencing books.book_id)
- quantity (int)
- order_date (date)
Table: payments
Columns:
- payment_id (int, primary key)
- order_id (int, foreign key referencing orders.order_id)
- payment_date (date)
- amount (decimal)
4
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
);
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
book_id INT,
quantity INT,
order_date DATE,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
FOREIGN KEY (book_id) REFERENCES books(book_id)
);
CREATE TABLE payments (
payment_id INT PRIMARY KEY,
order_id INT,
payment_date DATE,
amount DECIMAL(10, 2),
FOREIGN KEY (order_id) REFERENCES orders(order_id)
);
5
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Now, let's proceed with the questions and their corresponding SQL queries:
6
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Question 6: Retrieve the customer who has made the highest payment.
SELECT customers.customer_id, customers.name,
MAX(payments.amount) AS highest_payment
FROM customers
JOIN orders ON customers.customer_id = orders.customer_id
JOIN payments ON orders.order_id = payments.order_id;
7
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Question 9: Retrieve the customer who has made the most orders.
SELECT customers.customer_id, customers.name,
COUNT(orders.order_id) AS order_count
FROM customers
JOIN orders ON customers.customer_id = orders.customer_id
GROUP BY customers.customer_id
ORDER BY order_count DESC
LIMIT 1;
Question 10: Retrieve the customers who have not placed any orders.
SELECT customers.customer_id, customers.name
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id
WHERE orders.order_id IS NULL;
These are just a few examples of the kinds of queries you can perform on the
given database schema. Depending on your specific requirements, you can
create more complex queries to extract meaningful insights from the data.
8
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Table: employees
Columns:
- employee_id (int, primary key)
- first_name (varchar)
- last_name (varchar)
- department_id (int, foreign key referencing departments.department_id)
- salary (decimal)
Table: departments
Columns:
- department_id (int, primary key)
- department_name (varchar)
9
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Now, let's proceed with the questions and their corresponding SQL queries,
using a sample dataset:
10
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Question 5: Retrieve the employees who earn a salary higher than 55000.
SELECT * FROM employees WHERE salary > 55000;
11
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Question 9: Retrieve the department with the highest total salary expenditure.
SELECT departments.department_name, SUM(employees.salary) AS
total_salary_expenditure
FROM departments
JOIN employees ON departments.department_id =
employees.department_id
GROUP BY departments.department_name
ORDER BY total_salary_expenditure DESC
LIMIT 1;
Question 10: Retrieve the employees with the highest salary in each
department.
SELECT employees.first_name, employees.last_name,
departments.department_name, employees.salary
FROM employees
JOIN departments ON employees.department_id =
departments.department_id
WHERE (employees.department_id, employees.salary) IN (
SELECT department_id, MAX(salary)
12
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
FROM employees
GROUP BY department;
13
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Table: products
Columns:
- product_id (int, primary key)
- product_name (varchar)
- category_id (int, foreign key referencing categories.category_id)
- price (decimal)
Table: categories
Columns:
- category_id (int, primary key)
- category_name (varchar)
Table: customers
Columns:
- customer_id (int, primary key)
- customer_name (varchar)
- email (varchar)
- address (varchar)
Table: orders
Columns:
- order_id (int, primary key)
14
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Table: payments
Columns:
- payment_id (int, primary key)
- order_id (int, foreign key referencing orders.order_id)
- payment_date (date)
- amount (decimal)
Table: products
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(50),
category_id INT,
price DECIMAL(10,2),
FOREIGN KEY (category_id) REFERENCES categories(category_id)
);
Table: categories
15
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
category_name VARCHAR(50)
);
Table: customers
CREATE TABLE customers (
customer_id INT PRIMARY
KEY,
customer_name VARCHAR(50),
email VARCHAR(100),
address VARCHAR(100)
);
Table: orders
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
product_id INT,
quantity INT,
order_date DATE,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
FOREIGN KEY (product_id) REFERENCES products(product_id)
);
Table: payments
CREATE TABLE payments (
payment_id INT PRIMARY KEY,
order_id INT,
payment_date DATE,
16
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
amount DECIMAL(10,2),
FOREIGN KEY (order_id) REFERENCES orders(order_id)
);
Table: products
INSERT INTO products (product_id, product_name, category_id,
price) VALUES
(1, 'Laptop', 1, 1000),
(2, 'Smartphone', 2, 800),
(3, 'TV', 3, 1200),
(4, 'Camera', 4, 500);
Table: categories
INSERT INTO categories (category_id, category_name) VALUES
(1, 'Electronics'),
(2, 'Mobile'),
(3, 'Appliances'),
(4, 'Cameras');
Table: customers
INSERT INTO customers (customer_id, customer_name, email,
address) VALUES
(1, 'John Doe', '[email protected]', '123 Main St'),
(2, 'Jane Smith', '[email protected]', '456 Elm St'),
(3, 'Bob Johnson', '[email protected]', '789 Oak St');
17
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Table: orders
INSERT INTO orders (order_id, customer_id, product_id, quantity,
order_date) VALUES
(1, 1, 2, 2, '2023-05-01'),
(2, 2, 1, 1, '2023-05-02'),
(3, 3, 3, 3, '2023-05-03');
Table: payments
INSERT INTO payments (payment_id, order_id, payment_date,
amount) VALUES
(1, 1, '2023-05-02', 1600),
(2, 2, '2023-05-03', 1000),
(3, 3, '2023-05-04', 3600);
18
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
19
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Question 10: Retrieve the products with the highest price in each category.
SELECT products.product_name, products.price,
categories.category_name
FROM products
JOIN categories ON products.category_id = categories.category_id
WHERE (products.category_id, products.price) IN (
SELECT category_id, MAX(price)
FROM products
GROUP BY category_id
);
20
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Question 13: Retrieve the customers who have placed orders for a specific
product.
SELECT customers.customer_name, orders.quantity
FROM customers
JOIN orders ON customers.customer_id = orders.customer_id
JOIN products ON orders.product_id = products.product_id
WHERE products.product_name = '<product_name>';
(Note: Replace `<product_name>` with the actual product name you want to
retrieve customers for.)
21
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Question 16: Retrieve the most popular category (with the highest number of
products sold).
SELECT categories.category_name, COUNT(orders.order_id) AS
total_orders
FROM categories
JOIN products ON categories.category_id = products.category_id
JOIN orders ON products.product_id = orders.product_id
GROUP BY categories.category_name
ORDER BY total_orders DESC
LIMIT 1;
Question 17: Retrieve the customer with the highest number of orders.
SELECT customers.customer_name, COUNT(orders.order_id) AS
total_orders
FROM customers
JOIN orders ON customers.customer_id = orders.customer_id
GROUP BY customers.customer_name
ORDER BY total_orders DESC
LIMIT 1;
Question 18: Retrieve the total amount paid for each order.
SELECT orders.order_id, SUM(payments.amount) AS
total_amount_paid
FROM orders
JOIN payments ON orders.order_id = payments.order_id
22
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
GROUP BY orders.order_id;
Question 19: Retrieve the orders that have not been paid yet.
SELECT orders.order_id, customers.customer_name
FROM orders
JOIN customers ON orders.customer_id = customers.customer_id
LEFT JOIN payments ON orders.order_id = payments.order_id
WHERE payments.payment_id IS NULL;
23
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Table: orders
Table: products
24
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
25
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
26
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Answer:
| product_name | total_quantity |
|--------------|----------------|
| Laptop | 5 |
| Smartphone | 4 |
| TV | 4 |
Answer:
| product_name | average_quantity |
|--------------|------------------|
| Laptop | 2.5 |
| Smartphone | 2 |
| TV | 2 |
Question 13: What is the total price of products ordered by each customer?
SELECT customer_id, SUM(orders.quantity * products.price) AS
total_price
FROM orders
JOIN products ON orders.product_id = products.product_id
GROUP BY customer_id;
Answer:
| customer_id | total_price |
27
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
|-------------|-------------|
| 1 | 6800 |
| 2 | 3200 |
| 3 | 4200 |
28
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Question 17: What is the total quantity of products ordered per customer?
SELECT customer_id, SUM(quantity) AS total_quantity
FROM orders
GROUP BY customer_id;
Answer:
| customer_id | total_quantity |
|-------------|----------------|
| 1 | 6 |
| 2 | 2 |
| 3 | 5 |
Question 18: What is the average price of products ordered per customer?
SELECT customer_id, AVG(products.price) AS average_price
29
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
FROM orders
JOIN products ON orders.product_id = products.product_id
GROUP BY customer_id;
Answer:
| customer_id | average_price |
|-------------|---------------|
| 1 | 1133.3333 |
| 2 | 900 |
| 3 | 1000 |
Question 20: What is the average quantity of products ordered per customer?
SELECT AVG(quantity) AS average_quantity
FROM orders
GROUP BY customer_id;
Answer:
| average_quantity |
|------------------|
| 3.0000 |
| 1.0000 |
| 2.5000 |
30
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Table: sales
Question 1: What is the total quantity of products sold on each sale date?
SELECT sale_date, SUM(quantity) OVER (PARTITION BY sale_date) AS
total_quantity
FROM sales;
Answer:
| sale_date | total_quantity |
|------------|----------------|
| 2023-01-01 | 33 |
| 2023-01-02 | 37 |
31
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Question 2: What is the running total of quantity sold for each product?
SELECT product_id, sale_date, quantity,
SUM(quantity) OVER (PARTITION BY product_id ORDER BY
sale_date) AS running_total
FROM sales;
Answer:
| product_id | sale_date | quantity | running_total |
|------------|------------|----------|---------------|
| 1 | 2023-01-01 | 10 | 10 |
| 1 | 2023-01-02 | 5 | 15 |
| 2 | 2023-01-01 | 8 | 8 |
| 2 | 2023-01-02 | 12 | 20 |
| 3 | 2023-01-01 | 15 | 15 |
| 3 | 2023-01-02 | 20 | 35 |
Question 3: What is the rank of each product based on the total quantity
sold?
SELECT product_id, SUM(quantity) AS total_quantity,
RANK() OVER (ORDER BY SUM(quantity) DESC) AS product_rank
FROM sales
GROUP BY product_id;
Answer:
| product_id | total_quantity | product_rank |
|------------|----------------|--------------|
| 3 | 35 | 1 |
| 2 | 20 | 2 |
| 1 | 15 | 3 |
32
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Question 4: What is the dense rank of each product based on the total
quantity sold?
SELECT product_id, SUM(quantity) AS total_quantity,
DENSE_RANK() OVER (ORDER BY SUM(quantity) DESC) AS
product_dense_rank
FROM sales
GROUP BY product_id;
Answer:
| product_id | total_quantity | product_dense_rank |
|------------|----------------|--------------------|
| 3 | 35 | 1 |
| 2 | 20 | 2 |
| 1 | 15 | 3 |
33
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
34
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Question 10: What is the cumulative sum of quantity sold per product?
SELECT product_id, sale_date, quantity,
SUM(quantity) OVER (PARTITION BY product_id ORDER BY
sale_date) AS cumulative_sum
FROM sales;
35
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Answer:
| product_id | sale_date | quantity | cumulative_sum |
|------------|------------|----------|----------------|
| 1 | 2023-01-01 | 10 | 10 |
| 1 | 2023-01-02 | 5 | 15 |
| 2 | 2023-01-01 | 8 | 8 |
| 2 | 2023-01-02 | 12 | 20 |
| 3 | 2023-01-01 | 15 | 15 |
| 3 | 2023-01-02 | 20 | 35 |
Question 11: What is the first sale date for each product?
SELECT product_id, sale_date,
FIRST_VALUE(sale_date) OVER (PARTITION BY product_id
ORDER BY sale_date) AS first_sale_date
FROM sales;
Answer:
| product_id | sale_date | first_sale_date |
|------------|------------|-----------------|
| 1 | 2023-01-01 | 2023-01-01 |
| 1 | 2023-01-02 | 2023-01-01 |
| 2 | 2023-01-01 | 2023-01-01 |
| 2 | 2023-01-02 | 2023-01-01 |
| 3 | 2023-01-01 | 2023-01-01 |
| 3 | 2023-01-02 | 2023-01-01 |
Question 12: What is the last sale date for each product?
SELECT product_id, sale_date,
LAST_VALUE(sale_date) OVER (PARTITION BY product_id ORDER
BY sale_date) AS last_sale_date
36
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
FROM sales;
Answer:
| product_id | sale_date | last_sale_date |
|------------|------------|----------------|
| 1 | 2023-01-01 | 2023-01-02 |
| 1 | 2023-01-02 | 2023-01-02 |
| 2 | 2023-01-01 | 2023-01-02 |
| 2 | 2023-01-02 | 2023-01-02 |
| 3 | 2023-01-01 | 2023-01-02 |
| 3 | 2023-01-02 | 2023-01-02 |
Question 13: What is the cumulative sum of quantity sold for all products?
SELECT sale_date, quantity,
SUM(quantity) OVER (ORDER BY sale_date) AS cumulative_sum
FROM sales;
Answer:
| sale_date | quantity | cumulative_sum |
|------------|----------|----------------|
| 2023-01-01 | 10 | 10 |
| 2023-01-01 | 8 | 18 |
| 2023-01-01 | 15 | 33 |
| 2023-01-02 | 5 | 38 |
| 2023-01-02 | 12 | 50 |
| 2023-01-02 | 20 | 70 |
Question 14: What is the percentage of quantity sold compared to the total
quantity?
SELECT sale_date, quantity,
37
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Question 15: What is the difference in quantity sold compared to the average
quantity?
SELECT sale_date, quantity,
quantity - AVG(quantity) OVER () AS quantity_difference
FROM sales;
Answer:
| sale_date | quantity | quantity_difference |
|------------|----------|---------------------|
| 2023-01-01 | 10 | -2 |
| 2023-01-02 | 5 | -7 |
| 2023-01-01 | 8 | -4 |
| 2023-01-02 | 12 | 0 |
| 2023-01-01 | 15 | 3 |
| 2023-01-02 | 20 | 8 |
38
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Question 16: What is the maximum quantity sold for each product?
SELECT product_id, MAX(quantity) OVER (PARTITION BY product_id)
AS max_quantity
FROM sales;
Answer:
| product_id | max_quantity |
|------------|--------------|
| 1 | 10 |
| 1 | 10 |
| 2 | 12 |
| 2 | 12 |
| 3 | 20 |
| 3 | 20 |
Question 17: What is the minimum quantity sold for each product?
SELECT product_id, MIN(quantity) OVER (PARTITION BY product_id)
AS min_quantity
FROM sales;
Answer:
| product_id | min_quantity |
|------------|--------------|
| 1 | 5 |
| 1 | 5 |
| 2 | 8 |
| 2 | 8 |
| 3 | 15 |
| 3 | 15 |
39
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Question 18: What is the average quantity sold for each product?
SELECT product_id, AVG(quantity) OVER (PARTITION BY product_id)
AS average_quantity
FROM sales;
Answer:
| product_id | average_quantity |
|------------|------------------|
| 1 | 7.5 |
| 1 | 7.5 |
| 2 | 10 |
| 2 | 10 |
| 3 | 17.5 |
| 3 | 17.5 |
Question 19: What is the cumulative maximum quantity sold per product?
SELECT product_id, sale_date, quantity,
MAX(quantity) OVER (PARTITION BY product_id ORDER BY
sale_date) AS cumulative_max
FROM sales;
Answer:
| product_id | sale_date | quantity | cumulative_max |
|------------|------------|----------|----------------|
| 1 | 2023-01-01 | 10 | 10 |
| 1 | 2023-01-02 | 5 | 10 |
| 2 | 2023-01-01 | 8 | 8 |
| 2 | 2023-01-02 | 12 | 12 |
| 3 | 2023-01-01 | 15 | 15 |
| 3 | 2023-01-02 | 20 | 20 |
40
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Question 20: What is the cumulative minimum quantity sold per product?
SELECT product_id, sale_date, quantity,
MIN(quantity) OVER (PARTITION BY product_id ORDER BY
sale_date) AS cumulative_min
FROM sales;
Answer:
| product_id | sale_date | quantity | cumulative_min |
|------------|------------|----------|----------------|
| 1 | 2023-01-01 | 10 | 5 |
| 1 | 2023-01-02 | 5 | 5 |
| 2 | 2023-01-01 | 8 | 8 |
| 2 | 2023-01-02 | 12 | 8 |
| 3 | 2023-01-01 | 15 | 15 |
| 3 | 2023-01-02 | 20 | 15 |
41
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Here are 20 queries that involve joins and subqueries in MySQL, using the
tables 'item_master' and 'order_head', along with the necessary create and
insert statements for the tables and data:
42
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
43
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
44
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
11. Query to get orders and the highest-priced item in each order:
13. Query to get orders placed by customers who have ordered more than
once:
45
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
GROUP BY customer_name
HAVING COUNT(order_id) > 1
);
14. Query to get orders and the average unit price of items in each order:
15. Query to get orders and the most recent order date for each customer:
16. Query to get orders and the total number of items in each order:
46
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
17. Query to get orders and the total revenue generated from each order:
18. Query to get orders and the most expensive item in each order:
19. Query to get orders and the total number of unique items in each order:
20. Query to get orders and the customer name in uppercase for each order:
SELECT order_head.order_id, UPPER(order_head.customer_name) AS
customer_name
FROM order_head
JOIN item_master ON 1=1;
47
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Here are 20 MySQL queries that utilize STRING functions, along with the
necessary create table and insert statements for an "employee_details" table:
48
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
49
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
11. Query to extract a portion of employee email addresses after the '@'
symbol:
SELECT emp_name
FROM employee_details
WHERE emp_name LIKE 'J%';
50
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
51
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
52
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Here are 20 MySQL queries that utilize the CASE function, along with the
necessary create table and insert statements for a "cricket_players_details"
table:
53
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
4. Query to calculate the batting average for players based on runs scored
and matches played:
5. Query to display the country of players with the highest runs scored:
54
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
FROM cricket_players_details;
7. Query to calculate the total runs scored by players from each country:
SELECT country,
SUM(runs_scored) AS total_runs_scored
FROM cricket_players_details
GROUP BY country;
55
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
8. Query to display the player name and number of centuries scored, with a
label indicating if they are a "centurion":
10. Query to calculate the strike rate for players based on runs scored and
matches played:
56
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
END AS strike_rate
FROM cricket_players_details;
12. Query to display the player name and country, with a label indicating if
they have played international matches:
57
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
FROM cricket_players_details;
13. Query to calculate the average runs scored per match for players:
14. Query to categorize players based on their average runs per match:
58
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
15. Query to display the player name and country, with a label indicating if
they are from a specific country:
16. Query to calculate the average number of centuries scored per match for
players:
59
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
18. Query to display the player name and country, with a label indicating if
they are the highest run-scorer in their country:
19. Query to calculate the total number of players from each country and
assign a label indicating if it's a dominant cricketing nation:
60
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
61
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Here are 20 MySQL queries that utilize regular expressions (regex) functions,
along with the necessary create table and insert statements for a
"movie_details" table:
62
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
SELECT movie_title
FROM movie_details
WHERE movie_title REGEXP '^The';
SELECT movie_title
FROM movie_details
WHERE movie_title REGEXP 'tion$';
SELECT movie_title
FROM movie_details
WHERE movie_title REGEXP 'Godfather|Godfather II';
SELECT movie_title
FROM movie_details
WHERE movie_title REGEXP '^(\S+\s+){4}\S+$';
SELECT movie_title
FROM movie_details
63
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
SELECT movie_title
FROM movie_details
WHERE movie_title REGEXP '[0-9]';
SELECT movie_title
FROM movie_details
WHERE release_year REGEXP '199[0-9]';
10. Query to retrieve movies with directors whose names contain 'Tarantino':
SELECT movie_title
FROM movie_details
WHERE director REGEXP 'Tarantino';
11. Query to retrieve movies with ratings greater than or equal to 9.0:
SELECT movie_title
FROM movie_details
WHERE rating REGEXP '^[9-9]\.[0-9]$|^10\.0$';
64
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
12. Query to retrieve movies with titles containing exactly two consecutive
vowels:
SELECT movie_title
FROM movie_details
WHERE movie_title REGEXP '[aeiou]{2}';
13. Query to retrieve movies with genres starting with 'C' or 'D':
SELECT movie_title
FROM movie_details
WHERE genre REGEXP '^(C|D)';
14. Query to retrieve movies with directors having exactly two words in their
names:
SELECT movie_title
FROM movie_details
WHERE director REGEXP '^[^ ]+ [^ ]+$';
15. Query to retrieve movies with ratings between 8.0 and 8.9:
SELECT movie_title
FROM movie_details
WHERE rating REGEXP '^8\.[0-9]$';
65
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
16. Query to retrieve movies with titles containing the word 'Redemption':
SELECT movie_title
FROM
movie_details
WHERE movie_title REGEXP 'Redemption';
17. Query to retrieve movies with directors whose names end with 'Coppola':
SELECT movie_title
FROM movie_details
WHERE director REGEXP 'Coppola$';
18. Query to retrieve movies with genres containing the word 'Crime':
SELECT movie_title
FROM movie_details
WHERE genre REGEXP 'Crime';
19. Query to retrieve movies with titles starting with any letter except 'A':
SELECT movie_title
FROM movie_details
WHERE movie_title REGEXP '^[^A]';
66
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
20. Query to retrieve movies with ratings containing exactly one decimal digit:
SELECT movie_title
FROM movie_details
WHERE rating REGEXP '^[0-9]\.[0-9]$';
67
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Here are 20 MySQL queries that utilize date functions, along with the
necessary create table and insert statements for a "students_details" table:
SELECT student_name
FROM students_details
WHERE date_of_birth > '2002-01-01';
68
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
SELECT student_name
FROM students_details
WHERE YEAR(admission_date) = 2020;
SELECT CURDATE();
SELECT student_name
FROM students_details
WHERE MONTH(admission_date) = MONTH(CURDATE());
SELECT student_name
FROM students_details
WHERE DAYNAME(admission_date) = 'Friday';
8. Query to retrieve students with their age calculated based on the current
date:
SELECT student_name, TIMESTAMPDIFF(YEAR, date_of_birth,
CURDATE()) AS age
FROM students_details;
69
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
SELECT student_name
FROM students_details
WHERE MONTH(date_of_birth) = MONTH(CURDATE());
11. Query to retrieve the number of days between the date of birth and
admission date for each student:
12. Query to retrieve students who were admitted before a specific date:
SELECT student_name
FROM students_details
WHERE admission_date < '2021-01-01';
70
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
13. Query to retrieve students with their age calculated based on a specific
date:
15. Query to retrieve students who were born on a specific day of the week:
SELECT student_name
FROM students_details
WHERE DAYNAME(date_of_birth) = 'Monday';
16. Query to retrieve students with their admission year and month
combined:
71
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
SELECT student_name
FROM students_details
WHERE YEAR(date_of_birth) = 2001;
18. Query to retrieve students with their age calculated in months based on
the current date:
19. Query to retrieve students with their admission dates in a specific date
format:
20. Query to retrieve students who were admitted in a specific month and
year:
SELECT student_name
FROM students_details
WHERE YEAR(admission_date) = 2020 AND MONTH(admission_date) = 9;
72
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
Here are 20 MySQL queries that utilize numeric functions, along with the
necessary create table and insert statements for an "exam_score_details"
table:
73
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
7. Query to calculate the difference between math and science scores for each
student:
8. Query to calculate the square of the math score for each student:
74
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
9. Query to calculate the average score across all subjects for each student:
10. Query to round the math score to the nearest ten for each student:
11. Query to calculate the absolute difference between science and english
scores for each student:
12. Query to calculate the average score for each subject and round it to two
decimal places:
13. Query to calculate the square root of the math score for each student:
75
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
14. Query to calculate the percentage of the math score for each student out
of 100:
15. Query to calculate the ceiling value of the science score for each student:
16. Query to calculate the average score across all subjects and round it to
the nearest integer for each student:
17. Query to calculate the logarithm base 10 of the english score for each
student:
76
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
18. Query to calculate the average score for each subject and assign a grade
based on specific criteria:
SELECT student_name,
CASE
WHEN math_score >= 80 THEN 'A'
WHEN math_score >= 70 THEN 'B'
WHEN math_score >= 60 THEN 'C'
ELSE 'D'
END AS math_grade,
CASE
WHEN science_score >= 80 THEN 'A'
WHEN science_score >= 70 THEN 'B'
WHEN science_score >= 60 THEN 'C'
ELSE 'D'
END AS science_grade,
CASE
WHEN english_score >= 80 THEN 'A'
WHEN english_score >= 70 THEN 'B'
WHEN english_score >= 60 THEN 'C'
ELSE 'D'
END AS english_grade
FROM exam_score_details;
77
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it
SQL Practice Questions Queries with Answers
SELECT science_score
FROM exam_score_details
ORDER BY science_score
LIMIT 1 OFFSET (SELECT COUNT(*) FROM exam_score_details) / 2;
78
All right reserved by careerswitchhelp.com | Please refrain yourself of copying it