Skip to content
Exercises · Q5

Q.Using the sports database containing two relations (TEAM, MATCH_DETAILS) and write the queries for the following:

a) Display the MatchID of all those matches where both the teams have scored more than 70.
b) Display the MatchID of all those matches where FirstTeam has scored less than 70 but SecondTeam has scored more than 70.
c) Display the MatchID and date of matches played by Team 1 and won by it.
d) Display the MatchID of matches played by Team 2 and not won by it.
e) Change the name of the relation TEAM to T_DATA. Also change the attributes TeamID and TeamName to T_ID and T_NAME respectively.
Puducherry TnboardTextbookSubjective· 4mImportance★★★★★est
47% · 45/95 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Five SQL statements on the sports database created in Exercise 4. (a) and (b) are plain WHERE filters on the two score columns. (c) and (d) are the interesting ones: a team may play as FirstTeam or SecondTeam, so you must check both ID columns and compare the matching score column — one OR of two AND-conditions. "Not won" means "did not score more", i.e. lost or drew, so it is <=. (e) uses RENAME TABLE for the relation and ALTER TABLE … CHANGE for the attributes.

The schema and data we are querying

This question reuses the exact TEAM and MATCH_DETAILS tables built in Exercise 4 (Q.6), including the real MATCH_DETAILS rows printed there:

CREATE TABLE TEAM (
    TeamID   INT PRIMARY KEY,
    TeamName VARCHAR(50)
);

CREATE TABLE MATCH_DETAILS (
    MatchID          VARCHAR(3) PRIMARY KEY,
    MatchDate        DATE,
    FirstTeamID      INT,
    SecondTeamID     INT,
    FirstTeamScore   INT,
    SecondTeamScore  INT
);

MATCH_DETAILS (as inserted in Exercise 4):

MatchIDMatchDateFirstTeamIDSecondTeamIDFirstTeamScoreSecondTeamScore
M12018-07-17129086
M22018-07-18344548
M32018-07-19137856
M42018-07-19245667
M52018-07-18143287
M62018-07-17236751
Important

FirstTeamScore belongs to the team named in FirstTeamID, and SecondTeamScore to SecondTeamID. Pairing the wrong ID with the wrong score column is the single commonest mistake in this question.

(a) Matches where both teams scored more than 70

SELECT MatchID
FROM MATCH_DETAILS
WHERE FirstTeamScore > 70 AND SecondTeamScore > 70;

Output

MatchID
M1

Checking every row: M1 (90, 86 — both > 70 ✓), M2 (45, 48 — neither), M3 (78, 56 — second not > 70), M4 (56, 67 — neither > 70), M5 (32, 87 — first not > 70), M6 (67, 51 — neither > 70). Only M1 qualifies.

(b) FirstTeam scored less than 70, SecondTeam more than 70

SELECT MatchID
FROM MATCH_DETAILS
WHERE FirstTeamScore < 70 AND SecondTeamScore > 70;

Output

MatchID
M5

Only M5 (32, 87) has a first-team score under 70 and a second-team score over 70.

(c) Matches played by Team 1 and won by it — MatchID and date

Team 1 may be the first team or the second team, so both cases must be covered, each compared against its own score column:

SELECT MatchID, MatchDate
FROM MATCH_DETAILS
WHERE (FirstTeamID  = 1 AND FirstTeamScore  > SecondTeamScore)
   OR (SecondTeamID = 1 AND SecondTeamScore > FirstTeamScore);

Output

MatchIDMatchDate
M12018-07-17
M32018-07-19

Team 1 always plays as FirstTeamID in this data (it never appears in SecondTeamID) — in M1 (90 vs 86), M3 (78 vs 56) and M5 (32 vs 87). It won M1 and M3, and lost M5. So M1 and M3 qualify.

(d) Matches played by Team 2 and not won by it

"Not won" = the team did not score more than the opponent — it lost or drew. So the comparison uses <=:

SELECT MatchID
FROM MATCH_DETAILS
WHERE (FirstTeamID  = 2 AND FirstTeamScore  <= SecondTeamScore)
   OR (SecondTeamID = 2 AND SecondTeamScore <= FirstTeamScore);

Output

MatchID
M1
M4

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.