SQL Day 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

SUB-QUERIES:

DIFFERENT FORMS OF THE WHERE CLAUSE:


WHERE C1 AND C2...
WHERE C1 OR C2...
WHERE NOT C1 AND C2...
WHERE A IN...Each selected row must have a value of A in the list that follows IN.
NOT IN may be used to select all that are not in the list.
WHERE A OPERATOR ANY...Each selected row must satisfy the condition for any of the list
that
follows ANY. <,>,<=,>= and <>.
WHERE A OPERATOR ALL...Each selected row must satisfy the condition for any of the list
that
follows ALL. <,>,<=,>= and <>.
WHERE A LIKE x...
WHERE A BETWEEN x AND y...
WHERE A IS NULL...
WHERE EXISTS...
WHERE NOT EXISTS...

Match(MatchID,Team1,Team2,Ground,date,Winner)
Player(PlayerID,LName,Fname,Country,YBorn,BPlace,FTest)
Batting(MatchID,PID,Order,HOut,NRuns,Fours,Sixes,NBalls)
Bowling(MatchID,PID,NOvers,Maidens,NRuns,NWickets)

USING IN :

Q1> Find match Ids of all matches in the database in which Tendulkar batted.

select MatchID from Batting


where PID IN
(select PlayerId from Player
where Lname='Tendulkar');

Result of the query:


MatchID
2689

Equivalent join query:

select MatchID from Batting,Player


where PID=PlayerID
AND Lname='Tendulkar';

Result of the query:


MatchID
2689

Q2> Find Ids of players that have both bowled and batted in the ODI match 2689.

select PID from Batting


where MatchID=2689
AND PID IN
(select PID from Bowling
where MatchID=2689)

Q3> Find Ids of players that have either bowled or batted (or did both) in the ODI match 2689.

select PID from Batting


where MatchID=2689
OR PID IN
(select PID from Bowling
where MatchID=2689)

EQUIVALENT QUERY USING UNION:

select PID from Batting


where MatchID=2689
UNION
(select PID from Bowling
where MatchID=2689)

Q4> Find Ids of players who have batted as well as bowled in the ODI match 2689.

select PID from Batting


where MatchID=2689
INTERSECT
(select PID from Bowling
where MatchID=2689)

Q5> Only batted but did not bowl in match 2689.

select PID from Batting


where MatchID=2689
EXCEPT/MINUS
select PID from Bowling
where MatchID=2689

Equivalent sub-query...

select PID from Batting


where MatchID=2689
AND PID NOT IN
(select PID from Bowling
where MatchID=2689)

Q6> Find the match information of all matches in which Dhoni has batted.

select MatchID,Team1,Team2,Ground,Date from Match


where MatchID IN
(select MatchID from Batting
where PID IN
(select PlayerID from Player
where Lname='Dhoni'));
USING IS NULL:

Q7> Find player ids of those players whose date of first test match(FTest) is not given in the
database.

select PlayerID from Player


where FTest IS NULL;

USING ALL:

Q8> Find match IDs of those matches in which player 27001 bats and makes more runs than he
made
at every match he played at Melbourne.

select MatchID from Batting


where PID=27001 AND NRuns>ALL
(select NRuns from Batting
where PID=27001 AND matchID IN
(select MatchID from Match where ground='Melbourne'));

USING EXISTS & NOT EXISTS....

Q9> Find names of players who batted in match 2689.


select Fname,Lname from Player
where EXISTS
(select * from Batting
where PlayerID=PID
AND MatchID=2689)

Equivalent join query:

select Fname,Lname from Player,Batting


where PlayerID=PID
AND MatchID=2689;

You might also like