Test Index
Structured Query Language (SQL)
© examsnet.com
Question : 43 of 58
Marks:
+1,
-0
Consider the tables GAMES and PLAYERS given below :Table : GAMES
Table : PLAYERS
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.
| GCode | GameName | Type | Number | PrizeMoney |
|---|---|---|---|---|
| 101 | Carrom Board | Indoor | 2 | 5000 |
| 102 | Badminton | Outdoor | 2 | 12000 |
| 103 | Table Tennis | Indoor | 4 | NULL |
| 104 | Chess | Indoor | 2 | 9000 |
| 105 | Lawn Tennis | Outdoor | 4 | 25000 |
| PCode | Name | GCode |
|---|---|---|
| 1 | Nabi Ahmad | 101 |
| 2 | Ravi Sahai | 108 |
| 3 | Jatin | 101 |
| 4 | Nazneen | 103 |
[CBSE 2024 Compartment]
Solution:
(i)
SELECT Type, AVG(Number)FROM GAMESGROUP 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, NameFROM GAMES, PLAYERSWHERE 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 TypeFROM GAMES;
DISTINCT shows each type only once: Indoor, Outdoor.(iv)SELECT GameName, PrizeMoneyFROM GAMESWHERE 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
Go to Question: