Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL - DML Review with an example. 2 SELECT Statement SELECT [DISTINCT | ALL] {* | [columnExpression [AS newName]] [,...] } FROMTableName [alias] [,...]

Similar presentations


Presentation on theme: "SQL - DML Review with an example. 2 SELECT Statement SELECT [DISTINCT | ALL] {* | [columnExpression [AS newName]] [,...] } FROMTableName [alias] [,...]"— Presentation transcript:

1 SQL - DML Review with an example

2 2 SELECT Statement SELECT [DISTINCT | ALL] {* | [columnExpression [AS newName]] [,...] } FROMTableName [alias] [,...] [WHEREcondition] [GROUP BYcolumnList] [HAVINGcondition] [ORDER BYcolumnList]

3 3 SELECT Statement FROMSpecifies table(s) to be used. WHEREFilters rows. GROUP BYForms groups of rows with same column value. HAVINGFilters groups subject to some condition. SELECTSpecifies which columns are to appear in output. ORDER BY Specifies the order of the output.

4 4 SELECT Statement Order of the clauses cannot be changed. Only SELECT and FROM are mandatory.

5 Problem Consider the following database designed to maintain information for a national football league. TEAMS (Teamno, Teamname, Town) PLAYERS (Pno, Teamno, Lname, Fname, Pbdate, Coo) MATCHES (Matchno, Hteamno, Vteamno, Hgoals, Vgoals, Refno) REFEREES (Refno, Lname, Fname, Rbdate) Here most attributes have obvious meanings. The others are explained in the following: Pbdate: player’s birthdate Coo: Country of origin Hteamno: Home team’s number Vteamno: Visiting team’s number Rbdate: Referee’s birthdate Write the SQL statements for the following queries.

6 List the counts of players in all the teams in the league, organized by country of origin, only for foreign countries (i.e. country not ‘Turkey’). Print them out as shown below: COUNTRY PCOUNT France 4 … TEAMS (Teamno, Teamname, Town) PLAYERS (Pno, Teamno, Lname, Fname, Pbdate, Coo) MATCHES (Matchno, Hteamno, Vteamno, Hgoals, Vgoals, Refno) REFEREES (Refno, Lname, Fname, Rbdate)

7 List the first/last names of all the referees who refereed a game where at least one participating player had the same birthdate as the referee. TEAMS (Teamno, Teamname, Town) PLAYERS (Pno, Teamno, Lname, Fname, Pbdate, Coo) MATCHES (Matchno, Hteamno, Vteamno, Hgoals, Vgoals, Refno) REFEREES (Refno, Lname, Fname, Rbdate)

8 List the match numbers and referee names for those matches where the referee was younger than every player from the home team. TEAMS (Teamno, Teamname, Town) PLAYERS (Pno, Teamno, Lname, Fname, Pbdate, Coo) MATCHES (Matchno, Hteamno, Vteamno, Hgoals, Vgoals, Refno) REFEREES (Refno, Lname, Fname, Rbdate)

9 List the names of all teams and the total number of matches won by each (at home and away): TEAMNAME NUMWINS Fenerbahçe12 … TEAMS (Teamno, Teamname, Town) PLAYERS (Pno, Teamno, Lname, Fname, Pbdate, Coo) MATCHES (Matchno, Hteamno, Vteamno, Hgoals, Vgoals, Refno) REFEREES (Refno, Lname, Fname, Rbdate)

10 Find the top winners. Top winners are teams with the largest number of recorded wins for any team in the league. There can be more than one top winner team of course. TOPWINNERS Fenerbahçe Galatasaray … TEAMS (Teamno, Teamname, Town) PLAYERS (Pno, Teamno, Lname, Fname, Pbdate, Coo) MATCHES (Matchno, Hteamno, Vteamno, Hgoals, Vgoals, Refno) REFEREES (Refno, Lname, Fname, Rbdate)

11 List the names of all teams that won all the matches that they played as the home team. TEAMS (Teamno, Teamname, Town) PLAYERS (Pno, Teamno, Lname, Fname, Pbdate, Coo) MATCHES (Matchno, Hteamno, Vteamno, Hgoals, Vgoals, Refno) REFEREES (Refno, Lname, Fname, Rbdate)

12 List the count of matches that they refereed and the total number of goals in the matches that they refereed for each referee who is younger than 25 years old and refereed at least three games. TEAMS (Teamno, Teamname, Town) PLAYERS (Pno, Teamno, Lname, Fname, Pbdate, Coo) MATCHES (Matchno, Hteamno, Vteamno, Hgoals, Vgoals, Refno) REFEREES (Refno, Lname, Fname, Rbdate)


Download ppt "SQL - DML Review with an example. 2 SELECT Statement SELECT [DISTINCT | ALL] {* | [columnExpression [AS newName]] [,...] } FROMTableName [alias] [,...]"

Similar presentations


Ads by Google