Test Index

Structured Query Language (SQL) Get it on Google PlayDownload on the App Store CBSE Board Videos

© examsnet.com
Question : 43 of 58
Marks: +1, -0
Consider the tables GAMES and PLAYERS given below :
Table : GAMES
GCodeGameNameTypeNumberPrizeMoney
101Carrom BoardIndoor25000
102BadmintonOutdoor212000
103Table TennisIndoor4NULL
104ChessIndoor29000
105Lawn TennisOutdoor425000
Table : PLAYERS
PCodeNameGCode
1Nabi Ahmad101
2Ravi Sahai108
3Jatin101
4Nazneen103
Write SQL queries for the following :
(i) Display the game type and average number of games played in each type.
(ii) Display prize money, name of the game, and name of the players from the tables Games and Players.
(iii) Display the types of games without repetition.
(iv) Display the name of the game and prize money of those games whose prize money is known.
[CBSE 2024 Compartment]
Solution:  
(i)
SELECT Type, AVG(Number)
FROM GAMES
GROUP BY Type;
GROUP BY Type makes one group for Indoor and one for Outdoor; AVG(Number) gives the average in each group (Indoor: (2 + 4 + 2)/3 = 2.6667, Outdoor: (2 + 4)/2 = 3).
(ii)
SELECT PrizeMoney, GameName, Name
FROM GAMES, PLAYERS
WHERE GAMES.GCode = PLAYERS.GCode;
This is an equi-join: rows of the two tables are matched on the common column GCode (Nabi Ahmad and Jatin get Carrom Board, Nazneen gets Table Tennis; Ravi Sahai's GCode 108 has no matching game).
(iii)
SELECT DISTINCT Type
FROM GAMES;
DISTINCT shows each type only once: Indoor, Outdoor.
(iv)
SELECT GameName, PrizeMoney
FROM GAMES
WHERE PrizeMoney IS NOT NULL;
An unknown value is stored as NULL, and NULL is tested with IS NOT NULL (not with <> NULL). Table Tennis is left out.
© examsnet.com
Advertisements
Go to Question: