You are on page 1of 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