SQL Queries
SQL Queries
SELECT t1.column_name
FROM table_name t1
JOIN table_name t2 ON t1.column_name = t2.column_name
WHERE t1.primary_key <> t2.primary_key;
/*The statement uses a self-join to compare each row in the table with every
other row in the table*/
DELETE t1 FROM my_table t1, my_table t2 WHERE t1.id < t2.id AND t1.col =
t2.col;
/*Write SQL Query to find the ids of all the students are enrolled in the same
subjects.*/
SELECT id
FROM practice
WHERE subject IN (SELECT id FROM practice WHERE id = practice.id)
GROUP BY id
HAVING COUNT(DISTINCT subject) > 1;
SELECT e1.student_id
FROM enrollment e1
JOIN enrollment e2 ON e1.subject_id = e2.subject_id
WHERE e1.student_id <> e2.student_id
GROUP BY e1.student_id
HAVING COUNT(DISTINCT e2.student_id) > 1;
/*You have given employee and dept table and you have to find dept which have
max avg salary ?**/