0% found this document useful (0 votes)
4 views3 pages

DML SQL Problems and Solutions Guide

Uploaded by

shrishail.ad24
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)
4 views3 pages

DML SQL Problems and Solutions Guide

Uploaded by

shrishail.ad24
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

DML SQL Problems – Solutions

1. Insert a new student:

INSERT INTO Students (id, name, city, marks) VALUES (101, 'Rohan', 'Pune', 85);

2. Insert multiple rows:

INSERT INTO Students (id, name, city, marks) VALUES

(102, 'Meera', 'Mumbai', 90),

(103, 'Arun', 'Delhi', 75),

(104, 'Sita', 'Pune', 88);

3. Select all rows:

SELECT * FROM Students;

4. Select specific columns:

SELECT name, marks FROM Students;

5. Delete students living in Mumbai:

DELETE FROM Students WHERE city = 'Mumbai';

6. Update marks of Rahul:

UPDATE Students SET marks = 95 WHERE name='Rahul';

7. Increase all marks by 5:

UPDATE Students SET marks = marks + 5;

8. Delete student with lowest marks:

DELETE FROM Students WHERE marks = (SELECT MIN(marks) FROM Students);

9. Select students marks>80 desc:

SELECT * FROM Students WHERE marks>80 ORDER BY marks DESC;


10. Insert NULL row:

INSERT INTO Students (id, name, city, marks) VALUES (105, NULL, NULL, NULL);

11. Copy to TopStudents:

INSERT INTO TopStudents (id, name, marks)

SELECT id, name, marks FROM Students WHERE marks>85;

12. Update Orders with JOIN:

UPDATE Orders o JOIN Customers c ON o.customer_id=c.customer_id

SET [Link]=[Link];

13. Delete below average:

DELETE FROM Students WHERE marks < (SELECT AVG(marks) FROM Students);

14. Select 2nd highest:

SELECT marks FROM Students ORDER BY marks DESC LIMIT 1 OFFSET 1;

15. Insert if not exists:

INSERT INTO Students (id, name, marks)

SELECT 106, 'Kiran', 82

WHERE NOT EXISTS (SELECT 1 FROM Students WHERE id=106);

16. Find duplicates:

SELECT name, COUNT(*) FROM Students GROUP BY name HAVING COUNT(*)>1;

17. Delete duplicates keep one:

DELETE s1 FROM Students s1

JOIN Students s2 ON [Link]=[Link] AND [Link] > [Link];

18. Update salary below dept avg:


UPDATE Employees e

JOIN (SELECT department, AVG(salary) avg_salary FROM Employees GROUP BY department) t

ON [Link]=[Link]

SET [Link]=[Link]+2000

WHERE [Link] < t.avg_salary;

19. Insert computed (qty*price):

INSERT INTO SalesSummary (order_id, total_amount)

SELECT order_id, quantity*price FROM Sales;

20. Delete orders whose customer missing:

DELETE FROM Orders o

WHERE NOT EXISTS (SELECT 1 FROM Customers c WHERE o.customer_id=c.customer_id);

Common questions

Powered by AI

To retrieve the second highest value from a set of data in an SQL table, you can order the data and use LIMIT and OFFSET. For student marks, the query is: SELECT marks FROM Students ORDER BY marks DESC LIMIT 1 OFFSET 1 . This orders the marks in descending order and skips the highest to select the second highest mark.

To increase certain values for all rows in a table, an SQL UPDATE statement can be used with an arithmetic operation. For example, to increase all student marks by 5, the query is: UPDATE Students SET marks = marks + 5 . This updates every row in the Students table, adding 5 to the marks of each student.

A method to delete entries based on a calculated average involves using a subquery to calculate the average, then a DELETE statement based on that value. For student marks, this is done with: DELETE FROM Students WHERE marks < (SELECT AVG(marks) FROM Students). This deletes all students whose marks are below the average mark of all students in the table.

To copy data conditionally from one table to another based on a criterion, use an INSERT statement with a SELECT clause. For copying students with marks greater than 85, the query is: INSERT INTO TopStudents (id, name, marks) SELECT id, name, marks FROM Students WHERE marks>85 . This transfers only those students who meet the condition of having high marks.

To identify and select duplicate entries in an SQL table based on a criterion like student names, the GROUP BY clause along with HAVING can be used. The query is: SELECT name, COUNT(*) FROM Students GROUP BY name HAVING COUNT(*)>1 . This groups rows by the 'name' column and then selects groups with a count greater than 1, indicating duplicates.

To remove rows associated with missing references in another table, a subquery with a NOT EXISTS condition can be used in a DELETE statement. For example, to remove orders whose customers do not exist, the query is: DELETE FROM Orders o WHERE NOT EXISTS (SELECT 1 FROM Customers c WHERE o.customer_id=c.customer_id). This deletes orders without a matching customer ID in the Customers table.

To ensure a row reflecting the absence of certain data is added, an INSERT with NULL values can be performed. Example: INSERT INTO Students (id, name, city, marks) VALUES (105, NULL, NULL, NULL). This adds a row where 'name', 'city', and 'marks' are unknown, explicitly using NULL to denote absence.

SQL commands can update information across two related tables using a JOIN clause to relate them based on common keys. An example of this is updating the city in the Orders table based on the city information from the Customers table. The query to execute this is: UPDATE Orders o JOIN Customers c ON o.customer_id=c.customer_id SET o.city=c.city . This ensures consistency between related data in both tables.

In SQL, to delete duplicate rows based on a specific column while retaining only one instance, a self-join can be employed. For example, to remove duplicates from the Students table where duplicate entries are based on the 'name' column, the query is: DELETE s1 FROM Students s1 JOIN Students s2 ON s1.name=s2.name AND s1.id > s2.id . This deletes duplicate entries by comparing rows and keeping the entry with the smaller 'id'.

To insert data into a table only if a certain condition is not met, an "INSERT...SELECT" statement can be used in SQL, incorporating a WHERE clause with a NOT EXISTS condition. For the Students table, the query looks like: INSERT INTO Students (id, name, marks) SELECT 106, 'Kiran', 82 WHERE NOT EXISTS (SELECT 1 FROM Students WHERE id=106). This ensures that the insertion happens only if there is no student with id=106.

You might also like