SQL Interview Questions:
[Link] 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)
[Link] 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
[Link]>[Link])
[Link] Duplicate Rows from a Table.
delete from dummy where id not in(select min(id) from dummy group by name)
[Link] 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.
[Link] between DBMS and RDBMS.
[Link] 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 [Link],[Link]
from stu a, course b, stucourse c
where [Link]=[Link] and [Link]=[Link] and [Link]='Mike'
Output:
2nd Query:
Retrive count of all the Courses that are Joined by each student:
select [Link], count(*) as courses
from stu a, course b, stucourse c
where [Link]=[Link] and [Link]=[Link]
group by [Link]
Difference between Union And Union All
Union doesnt allows the Duplicates, But Union All will allow the Duplicates in the Result
Set.
View: