0% found this document useful (0 votes)
19 views

SQL Interview Questions

The document discusses several SQL interview questions and concepts: 1) How to find the second highest salary and Nth highest salary from a table using MAX, LIMIT, and ORDER BY. 2) How to delete duplicate rows from a table by removing rows with non-minimum IDs for each name. 3) The differences between DELETE, TRUNCATE, and DROP - how they remove rows and whether the operations can be rolled back. 4) The concept of a many-to-many relationship between tables and how to query courses joined by a particular student.

Uploaded by

venkateshgavini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

SQL Interview Questions

The document discusses several SQL interview questions and concepts: 1) How to find the second highest salary and Nth highest salary from a table using MAX, LIMIT, and ORDER BY. 2) How to delete duplicate rows from a table by removing rows with non-minimum IDs for each name. 3) The differences between DELETE, TRUNCATE, and DROP - how they remove rows and whether the operations can be rolled back. 4) The concept of a many-to-many relationship between tables and how to query courses joined by a particular student.

Uploaded by

venkateshgavini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL Interview Questions:

1.Finding Second Highest:

select max(sal) From emp where sal NOT IN (select max(sal) from emp);
select * from emp order by sal desc limit 1,1;(starting from 1(2nd record) to 1 record to
retrieve)

2.Finding Nth Highest:

select * from emp order by sal desc limit n-1,1;


select id,name,max(sal) from salary a where 2 = (select count(*) from salary b where
a.sal>b.sal)

3.Delete Duplicate Rows from a Table.

delete from dummy where id not in(select min(id) from dummy group by name)

3.Differance between Delete, Truncate and Drop.

The DROP command removes a table from the database. All the tables' rows, indexes
and privileges will also be removed. ... DROP and TRUNCATE are DDL commands,
whereas DELETE is a DML command. Therefore DELETE operations can be rolled
back (undone), while DROP and TRUNCATE operations cannot be rolled back.
TRUNCATE Removes all rows from a table or specified partitions of a table, without
logging the individual row deletions. TRUNCATE TABLE is similar to the DELETE
statement with no WHERE clause; however, TRUNCATE TABLE is faster and uses
fewer system and transaction log resource.
4.Differance between DBMS and RDBMS.

5.Many to Many RelationShip Entire Concept:

Table : Stu
Table : stucourse(It acts as a RelationShip between two Tables).

Table: Course

(i)Query:

Retrive the courses that are Joined by the Mike.

select a.sname,b.cname

from stu a, course b, stucourse c

where a.sid=c.stuid and b.cid=c.courseid and a.sname='Mike'

Output:
2nd Query:

Retrive count of all the Courses that are Joined by each student:

select a.sname, count(*) as courses


from stu a, course b, stucourse c
where a.sid=c.stuid and b.cid=c.courseid
group by a.sname

Difference between Union And Union All


Union doesnt allows the Duplicates, But Union All will allow the Duplicates in the Result
Set.
View:

You might also like