1 of 24
1. Write an SQL query to retrieve all records from the students table ?
Answer:-
Explanation:
• SELECT * means select all columns.
• FROM students tells the database to get the data from the students table.
Example: Let's say your students table looks like this:
student_id name age course maths_marks
1 Rahul 20 B.Sc 85
2 Anjali 21 B.Com 78
3 Sameer 19 B.Sc 92
Running this query:
Would return:
student_id name age course maths_marks
1 Rahul 20 B.Sc 85
2 Anjali 21 B.Com 78
3 Sameer 19 B.Sc 92
2 of 24
2. Write an SQL query to display the names and email addresses of employees from the
employee table.
Answer:-
Explanation:
• SELECT name, email: Retrieves only the name and email columns.
• FROM employee: Specifies the employee table to fetch data from.
Example: Assume your employee table contains the following data:
emp_id name email department
101 Priya [email protected] Sales
102 Raj [email protected] HR
103 Sneha [email protected] IT
Running the query:
Will return:
name email
Priya [email protected]
Raj [email protected]
Sneha [email protected]
3. Write a query to list all products from the product table whose price is greater than 500.
Answer:-
3 of 24
Explanation:
• SELECT *: Retrieves all columns for matching records.
• FROM product: Specifies the table named product.
• WHERE price > 500: Filters records where the price is greater than 500.
Example: Assume your product table contains the following data -
product_id product_name price
1 Laptop 55000
2 Mouse 450
3 Monitor 8000
4 Pen Drive 300
Running the query:
Will return:
product_id product_name price
1 Laptop 55000
3 Monitor 8000
4. Write a query to find the details of customers who live in ‘Mumbai'.
Answer:-
4 of 24
Explanation:
• SELECT *: Retrieves all columns for matching customers.
• FROM customers: Specifies the customers table.
• WHERE city = 'Mumbai': Filters only those customers whose city is Mumbai.
Example: Assume your customers table contains the following data:
customer_id name email city
Running the query:
Will return:
customer_id name email city
1 Ramesh [email protected] Mumbai
3 Anil [email protected] Mumbai
5. Display the name and department of all employees sorted by name in ascending order.
Answer:-
5 of 24
Explanation:
• SELECT name, department: Retrieves only the name and department columns.
• FROM employee: Specifies the employee table.
• ORDER BY name ASC: Sorts the result alphabetically by name in ascending order
(ASC is optional as it is the default).
Example: Assume your employee table contains the following data:
emp_id name department
1 Rahul Sales
2 Anjali HR
3 Sneha IT
4 Mohan Finance
Running the query:
Will return:
name department
Anjali HR
Mohan Finance
Rahul Sales
Sneha IT
6. Write a query to display the total salary paid to employees in the sales department ?
Answer:-
6 of 24
Explanation:
• SUM(salary): Adds up all salary values.
• WHERE department = 'Sales': Restricts the query to only employees in the Sales
department.
• AS total_salary: Gives the result column a clear label.
Example: Assume the employee table contains:
emp_id name department salary
1 Rahul Sales 30000
2 Sneha IT 40000
3 Anjali Sales 35000
4 Mohan HR 28000
Running the query:
Would return:
Total_salary = 65000
Because:
Rahul (Sales): 30000
Anjali (Sales): 35000, Total = 30000 + 35000 = 65000
7. Write a query to find the maximum and minimum salaries from the employee table.
Answer:-
Explanation:
7 of 24
• MAX(salary): Returns the highest salary in the table.
• MIN(salary): Returns the lowest salary in the table.
• AS max_salary / min_salary: Renames the output columns for readability.
Example: Assume your employee table contains the following data:
emp_id name department salary
1 Rahul Sales 30000
2 Sneha IT 45000
3 Anjali Sales 28000
4 Mohan HR 50000
Running the query:
Will return:
max_salary min_salary
50000 50000
8. Write an SQL query to count the number of students in each department.
Answer:-
8 of 24
Explanation:
•COUNT(*): Counts the number of students in each group.
•GROUP BY department: Groups students by their department.
•AS student_count: Gives a clear label to the result column.
Example: Assume the students table contains the following data:
student_id name department
1 Riya B.Sc
2 Karan B.Com
3 Sneha B.Sc
4 Mohit BBA
5 Pooja B.Sc
6 Alok B.Com
Running the query:
Will return:
department student_count
B.Sc 3
B.Com 2
BBA 1
9. Write a query to display the names of students who have scored more than 80 marks in Maths.
Answer:-
9 of 24
Explanation:
• SELECT name: Retrieves the names of students.
• FROM students: Specifies the students table.
• WHERE maths_marks > 80: Filters only those students whose Maths marks are greater
than 80.
Example: Assume the students table contains:
student_id name maths_marks
1 Riya 78
2 Karan 85
3 Sneha 92
4 Mohit 65
Running the query:
Will return:
name
Karan
Sneha
10. Write a query to update the price of all products in the product table by increasing it by 10%.
Answer:-
Explanation:
10 of 24
• UPDATE product: Targets the product table.
• SET price = price * 1.10: Increases each product's price by 10% (i.e., multiplies by
1.10).
Example: Assume your product table looks like this before the update:
product_id product_name price
1 Laptop 50000
2 Mouse 1000
3 Monitor 8000
Running the query:
Will return:
product_id product_name price
1 Laptop 55000
2 Mouse 1100
3 Monitor 8800
11. Write a query to display the names of employees who joined in the year 2023.
Answer:-
11 of 24
Explanation:
• SELECT name: Fetches employee names.
• FROM employee: Uses the employee table.
• WHERE YEAR(join_date) = 2023: Filters employees whose join_date falls in the year
2023.
Example: Assume the employee table contains:
emp_id name join_date
1 Riya 2022-08-15
2 Karan 2023-03-10
3 Sneha 2023-07-25
4 Mohit 2024-01-05
Running the query:
Will return:
name
Karan
Sneha
12. Write an SQL query to display employees whose names start with the letter 'A'.
Answer:-
12 of 24
Explanation:
•LIKE 'A%': Finds names that start with the letter ‘A'.
•% is a wildcard that matches any number of characters after 'A'.
•SELECT *: Retrieves all columns from the employee table.
Example: Assume your employee table contains:
emp_id name department salary
1 Anjali HR 30000
2 Amit Sales 32000
3 Raj IT 40000
4 Sneha Finance 35000
Running the query:
Will return:
emp_id name department salary
1 Anjali HR 30000
2 Amit Sales 32000
13. Write a query to find employees who earn more than the average salary of all employees.
Answer:-
13 of 24
Explanation:
• The subquery (SELECT AVG(salary) FROM employee) calculates the average salary of
all employees.
• The main query selects all employee records where the salary is greater than that
average.
Example: Assume your employee table has the following data:
emp_id name salary
1 Riya 30000
2 Karan 40000
3 Sneha 35000
4 Mohit 50000
Average salary = (30000 + 40000 + 35000 + 50000) / 4 = 38750
Running the query:
Will return:
emp_id name salary
2 Karan 40000
4 Mohit 50000
14 of 24
14. Write an SQL join query to display order details along with customer name from order
customers tables.
Answer:-
15 of 24
Explanation:
• JOIN customers ON orders.customer_id = customers.customer_id: Joins the two tables
using the foreign key relationship.
• SELECT ...: Retrieves order details along with the customer name.
• AS customer_name: Renames the column for readability.
Example: Customers Table:
Customers Table:
customer_id name city
1 Riya Mumbai
2 Karan Delhi
3 Sneha Pune
Orders Table:
order_id customer_id order_date amount
101 1 2024-01-12 5000
102 2 2024-02-18 3000
103 1 2024-03-05 7000
Running the query:
Will return:
order_id order_date amount customer_name
16 of 24
101 2024-01-12 5000 Riya
102 2024-02-18 3000 Karan
103 2024-03-05 7000 Riya
15. Write a nested query to find the names of students who scored the highest marks in the exam
table.
Answer:-
Explanation:
• The inner query SELECT MAX(marks) FROM exam finds the highest mark.
• The outer query returns the names of students whose marks equal that maximum.
Example: Assume your exam table looks like this:
17 of 24
student_id name subject marks
1 Riya Maths 88
2 Karan Maths 95
3 Sneha Maths 91
4 Mohit Maths 95
Running the query:
Will return:
name
Karan
Mohit
Because both scored the highest mark: 95.
18 of 24
16. Write SQL commands to:
a) Create a table department with columns dept_id, dept_name, and location.
b) Insert a record into the department table.
Answer:-
a) Create a Table department
Explanation:
• dept_id: Integer type and used as the primary key.
19 of 24
• dept_name and location: String (text) fields with a max length of 50 characters.
b) Insert a Record into the department Table
Explanation:
• Adds one record to the table with:
◦ dept_id = 101
◦ dept_name = Human Resources
◦ location = Mumbai
After Insert, the Table Will Look Like:
dept_id dept_name location
101 Human Resources Mumbai
17. Write a command to delete all records from the orders table where the order date is before
'2024-01-01'.
Answer:-
Explanation:
• DELETE FROM orders: Specifies the table from which records will be removed.
• WHERE order_date < '2024-01-01': Filters records to delete only those with an order
date before January 1, 2024.
Example: Before Deletion – orders Table:
order_id customer_id order_date amount
101 1 2023-12-28 5000
102 2 2024-01-05 3000
20 of 24
103 3 2023-11-15 7000
Running the query:
Updated orders Table:
order_id customer_id order_date amount
102 2 2024-01-05 3000
18. Alter the employee table to add a new column email.
Answer:-
Explanation:
• ALTER TABLE employee: Targets the table named employee.
• ADD email VARCHAR(100): Adds a new column named email with a maximum length
of 100 characters (suitable for email addresses).
Example: Before Altering — employee Table:
emp_id name department salary
1 Riya HR 30000
2 Karan IT 40000
21 of 24
Running the query:
Will return:
After Altering — employee Table:
emp_id name department salary email
1 Riya HR 30000 NULL
2 Karan IT 40000 NULL
The new email column is added, and existing rows will show NULL for email until updated.
22 of 24
19. Count the number of students enrolled in each course and display only those courses where
more than 5 students are enrolled.
Answer:-
Explanation:
• GROUP BY course: Groups students by course.
• COUNT(*): Counts the number of students in each course.
• HAVING COUNT(*) > 5: Filters only those courses with more than 5 students.
Example: students Table:
student_id name course
23 of 24
1 Riya BCA
2 Karan BCA
3 Sneha BCom
4 Mohit BCA
5 Priya BCom
6 Ankit BCA
7 Meena BCA
8 Rahul BCom
9 Simran BCom
10 Neha BCom
11 Arjun BCom
Running the query:
course student_count
BCA 5
BCom 6
So only BCom will be shown in the result.
20. Display the average marks scored in each subject, and sort the result by average marks in
descending order.
Answer:-
Explanation:
• AVG(marks): Calculates the average marks per subject.
• GROUP BY subject: Groups the results by each subject.
24 of 24
• ORDER BY average_marks DESC: Sorts the results in descending order of average
marks.
Example: exam Table:
student_id name subject marks
1 Riya Maths 90
2 Karan Maths 80
3 Sneha Science 85
4 Mohit English 75
5 Neha Science 95
6 Rahul English 65
Running the query:
subject average_marks
Science 90.00
Maths 85.00
English 70.00