You are on page 1of 2

1.Suppse you have a table called students with columns id, name, age, and grade.

Write an SQL query


to insert a new student with id 1001, name “Alice”, age 18, and grade “A”.

ANS: INSERT INTO students VALUES(‘1001’,’alice’,’18’,’A’);

2. Write an SQL query to update the age of student with id 1001 to 19 in the students table.

ANS: UPDATE student SET AGE=19 WHERE ID= 1001;

3.Write an SQL query to delete the student with id 1001 from the students table.

ANS: DELETE FROM student WHERE ID = 1001;

4. Suppose you have two tables called customers and orders. The customers table has columns customer
id, first_ name, and last_ name. The orders table has columns order_ id, customer _id, and order_
total. Write an SQL query to join these two tables on the customer _id column and return a list of all
customers with their total order amounts.

ANS SELECT c.customer_ id, c.first_name, c.last_name, SUM(o.order_total) AS total_order_amount

FROM customers c

JOIN orders o ON c.customer_id = o.customer_id

GROUP BY c.customer_id, c.first_name, c.last_name;

5. Suppose you have two tables called employees and salaries. The employees table has columns emp_
no, first_ name, last _name, and dep t_no. The salaries table has columns emp _no and salary. Write
an SQL query to calculate the average salary of all employees in each department.

ANS; SELECT e. dept_ no, AVG (s. salary) AS average _salary

FROM employees e

JOIN salaries s ON e. emp_ no = s. emp_ no

GROUP BY e. dept_ no;

6. Write an SQL query to find the total number of orders placed by each customer in the orders table.

ANS: SELECT customer_id, COUNT(order_id) AS total_orders

FROM orders

GROUP BY customer_id;

7. Write an SQL query to find the highest salary of employees in each department in
the employees and salaries tables.

ANS; SELECT e.dept_no, MAX(s.salary) AS highest_salary


FROM employees e
JOIN salaries s ON e.emp_no = s.emp_no
GROUP BY e.dept_no;
8. Suppose you have a table called products with columns product _id, product _name, and price. Write
an SQL query to find the total revenue generated by each product in the orders table, assuming each
order contains only one product.

ANS: SELECT p. product_ id, p. product _name, SUM(p. price) AS total _revenue

FROM products p

JOIN orders o ON p. product_ id = o. product_ id

GROUP BY p. product_ id, p. product_ name;

9. Write an SQL query to find the average age of male and female students in the students table using
UNION.

ANS: SELECT 'Male' AS gender, AVG(age) AS average_age

FROM students

WHERE gender = 'Male'

UNION

SELECT 'Female' AS gender, AVG(age) AS average_age

FROM students

WHERE gender = 'Female';

10. Suppose you have a table called sales with columns sale_id, date, and amount. Write an SQL query
to find the total amount of sales for each month and year combination.

ANS: SELECT YEAR(date) AS Year, MONTH(date) AS Month ,SUM(AMOUNT)AS Total_

FROM SALES

GROUP BY YEAR(date), MONTH( date) ;

You might also like