DML SQL Problems and Solutions Guide
DML SQL Problems and Solutions Guide
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.