You are on page 1of 36

INTRODUCTION TO SQL

Teknologi Informasi Politeknik Negeri Padang

Tables (1)
Table Players

Tables (2)
Table Teams Table Matches

Table Penalties

Tables (3)
Table Committee_members

Database schema (1)


PLAYERS
playerno smallint not null PK name char(15) not null initials char(3) not null birth_date date . . . leaugueno char(4)

TEAMS
teamno smallint not null PK playerno smallint not null

division char(6) not null

MATCHES
matchno smallint not null PK teamno smallint not null playerno smallint not null won smallint not null lost smallint not null

Database schema (2)


PENALTIES PAYMENTNO PLAYERNO PAYMENT_DATE AMOUNT PLAYERS PLAYERNO NAME INITIALS BIRTH_DATE SEX JOINED STREET HOUSENO POSTCODE TOWN PHONENO LEAGUENO smallint <pk> not null not null char(15) not null char(3) null datetime not null char(1) not null smallint not null char(15) null char(4) null char(6) not null char(10) null char(10) null char(4) MATCHNO TEAMNO PLAYERNO WON LOST <pk> not null int <fk> not null smallint not null datetime not null decimal(7,2)

MATCHES smallint <pk> smallint <fk1> smallint <fk2> smallint smallint not not not not not null null null null null TEAMS smallint <pk> not null TEAMNO not null PLAYERNO smallint not null char(6) DIVISION

COMMITTEE_MEMBERS PLAYERNO BEGIN_DATE END_DATE POSITION smallint <pk,fk> not null not null datetime <pk> null datetime null char(20)

Other constraints
Column value constraints:
SEX IN {'M','F'} WON, LOST IN {0,1,2,3} POSITION IN {'Chairman', 'Secretary', 'Treasurer', 'General member'}

Row level constraints:


Table PLAYERS: YEAR (BIRTH_DATE) <= JOINED Table COMMITTEE_MEMBERS: END_DATE >= BEGIN_DATE

SQL
SQL consists of: Data Definition Language (DDL)
CREATE TABLE, ALTER TABLE, DROP TABLE

Data Manipulation Language (DML)


INSERT, SELECT, UPDATE, DELETE

Data Control Language (DCL)


GRANT, REVOKE

SQL
SQL consists of: Data Definition Language (DDL)
CREATE TABLE, ALTER TABLE, DROP TABLE

Data Manipulation Language (DML)


INSERT, SELECT, UPDATE, DELETE

Data Control Language (DCL)


GRANT, REVOKE

SELECT-statement (1)
Example 8.5: Get the number, name, sex and birth date of each male player born after 1970. Sort the result by name and birth_date. Query:

SELECT FROM WHERE AND ORDER BY


Result:

playerno, name, sex, birth_date Players sex = M Year(birth_date) > 1970 name, birth_date

PLAYERNO NAME 57 Brown

SEX M

BIRTH_DATE 1971-08-17

SELECT-statement (2)
Example 10.2: For each town find the number of players Query:
SELECT town, COUNT (*) FROM Players GROUP BY town;

Result after grouping (before applying SELECT):


PLAYERNO 95 104 27 44 8 28 112 100 83 57 39 7 6 2 NAME Miller Moorman Collins Baker Newcastle Collins Bailey Parmenter Hope Browne Bishop Wise Parmenter Everett TOWN Douglas Eltham Inglewood Midhurst Plymouth Stratford

SELECT-statement (3)
Example of section 6.2: Get the number of each player who has incurred more than one penalty with an amount more than 25.

Query:
SELECT FROM WHERE GROUP BY HAVING ORDER BY playerno Penalties amount > 25 playerno COUNT(*) > 1 playerno

SELECT-statement (4)
Example: List for each player who has incurred one or more penalties the playernumber and the number of penalties. Order descending on the number of penalties and playerno. Query:
SELECT FROM GROUP BY ORDER BY playerno, COUNT(*) Penalties playerno 2 DESC, 1

clauses SELECT-statement

FROM WHERE GROUP BY HAVING SELECT

defines the source tables selects rows that satisfy the condition(s) groups rows on base of equal values in columns selects groups that satisfy the condition(s) select column(s)

ORDER BY

sorts rows on values in column(s)

logical operators
Example: Which male players joined in 1979 or 1980? List player number, name, sex and the year joined Query:
SELECT * FROM Players WHERE sex = M AND joined = 1979 OR joined = 1980

Result:
PLAYERNO 8 39 44 100 NAME Newcastle Bishop Baker Parmenter SEX F M M M JOINED 1980 1980 1980 1979

What is wrong?

Join of two tables (1)


Example 7.3: Find the team number and the name of the captain of each team. Query:
SELECT FROM WHERE TEAMNO, NAME TEAMS, PLAYERS TEAMS.PLAYERNO = PLAYERS.PLAYERNO;

Result:
TEAMNO NAME 1 Parmenter 2 Collins

Join condition

Join of two tables (2)


Example 7.3: Find the team number and the name of the captain of each team.
SELECT FROM WHERE TEAMNO, NAME TEAMS, PLAYERS TEAMS.PLAYERNO = PLAYERS.PLAYERNO;

Result of Cartesian product


SELECT FROM * TEAMS, PLAYERS;

is displayed on next slide.

Join of two tables (3)


TEAMNO 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 PLAYERNO 6 6 6 6 6 6 6 6 6 6 6 6 6 6 27 27 27 27 27 27 27 27 27 27 27 27 27 27 DIVISION first first first first first first first first first first first first first first second second second second second second second second second second second second second second PLAYERNO 2 6 7 8 27 28 39 44 57 83 95 100 104 112 2 6 7 8 27 28 39 44 57 83 95 100 104 112 NAME Everett Parmenter Wise Newcastle Collins Collins Bishop Baker Brown Hope Miller Parmenter Moorman Bailey Everett Parmenter Wise Newcastle Collins Collins Bishop Baker Brown Hope Miller Parmenter Moorman Bailey INITIALS R R GWS B DD C D E M PK P P D IP R R GWS B DD C D E M PK P P D IP

Alternative join syntax (1)


Example 7.3: Find the team number and the name of the captain of each team.
SELECT TEAMNO, NAME FROM TEAMS INNER JOIN PLAYERS ON TEAMS.PLAYERNO = PLAYERS.PLAYERNO;

Join of two tables (4)


Example 7.4: For each penalty, find the payment number, the amount of penalty, the player number and the name and initials of the player who incurred the penalty. Query:
SELECT FROM WHERE PAYMENTNO, AMOUNT, PENALTIES.PLAYERNO, NAME, INITIALS PENALTIES, PLAYERS PENALTIES.PLAYERNO = PLAYERS.PLAYERNO;

With pseudonyms:
SELECT FROM WHERE PAYMENTNO, AMOUNT, PEN.PLAYERNO, NAME, INITIALS PENALTIES PEN, PLAYERS P PEN.PLAYERNO = P.PLAYERNO;

Alternative join syntax (2)


Example 7.4: For each penalty, find the payment number, the amount of penalty, the player number and the name and initials of the player who incurred the penalty. With INNER JOIN syntax:
SELECT FROM PAYMENTNO, AMOUNT, PENALTIES.PLAYERNO, NAME, INITIALS PENALTIES INNER JOIN PLAYERS ON PENALTIES.PLAYERNO = PLAYERS.PLAYERNO;

With pseudonyms:
SELECT FROM PAYMENTNO, AMOUNT, PEN.PLAYERNO, NAME, INITIALS PENALTIES PEN INNER JOIN PLAYERS P ON PEN.PLAYERNO = P.PLAYERNO;

Query with subquery (1)


Example 8.18: Get the player number, name and initials of each player who has played at least one match. Query:
SELECT PLAYERNO, NAME, INITIALS FROM PLAYERS WHERE PLAYERNO IN ( SELECT PLAYERNO FROM MATCHES );

Alternative solution with a join:


SELECT DISTINCT M.PLAYERNO, NAME, INITIALS FROM MATCHES AS M, PLAYERS AS P WHERE M.PLAYERNO = P.PLAYERNO;

Query with subquery (2)


Example: Get the player number, name and initials of each leaugue player who did not yet play a match for team 1. Query:
SELECT PLAYERNO, NAME, INITIALS FROM PLAYERS WHERE PLAYERNO NOT IN ( SELECT PLAYERNO FROM MATCHES WHERE TEAMNO = 1 );

Query with subquery (3)


Example 8.26: Give the player number(s), name(s) and date(s) of birth of the oldest player(s).
SELECT FROM WHERE PLAYERNO, NAME, BIRTH_DATE PLAYERS BIRTH_DATE = (SELECT MIN (BIRTH_DATE) FROM PLAYERS);

Alternative (note: the oldest player(s) is(are) those whose date of birth is less than or equal to that of every player, themselves included:
SELECT FROM WHERE PLAYERNO, NAME, BIRTH_DATE PLAYERS BIRTH_DATE <= ALL (SELECT BIRTH_DATE FROM PLAYERS);

Result:
PLAYERNO 2 NAME Everett BIRTH_DATE 1948-09-01

More DML
DML:
SELECT INSERT UPDATE DELETE

INSERT (1)
Example 15.1: A new team has enrolled in the league. This third team will be captained by player 100 and will compete in the third division. SQL statement:
INSERT INTO TEAMS (TEAMNO, PLAYERNO, DIVISION) VALUES (3, 100, 'third');

Also possible:
INSERT INTO TEAMS (TEAMNO, DIVISION, PLAYERNO) VALUES (3, 'third', 100);

Also possible:
INSERT INTO TEAMS VALUES (3, 100, 'third');

INSERT (2)
Example 15.2: Make a separate table in which the number, name, town and telephone number of each non-competition player is recorded. SQL statements:
CREATE TABLE (PLAYERNO NAME TOWN PHONENO PRIMARY KEY RECR_PLAYERS SMALLINT NOT NULL, CHAR(15) NOT NULL, CHAR(10) NOT NULL, CHAR(10) , (PLAYERNO) );

INSERT INTO RECR_PLAYERS SELECT PLAYERNO, NAME, TOWN, PHONENO FROM PLAYERS WHERE LEAGUENO IS NULL;

UPDATE
Example 15.7: The Parmenter family has moved house to 83 Palmer Street in Inglewood; the postal code has become 1234UU and the telephone number is unknown. SQL statement:
UPDATE SET PLAYERS STREET = 'Palmer Street',

HOUSENO
TOWN PHONENO WHERE NAME

= '83',
= 'Inglewood', = NULL = 'Parmenter';

POSTCODE = '1234UU',

DELETE
Example: Delete all penalties with an amount less than the average amount of penalties. Delete statement:
DELETE FROM WHERE PENALTIES AMOUNT < (SELECT FROM AVG (AMOUNT) PENALTIES);

CREATE TABLE (1)


Example: Create the table Players.

SQL statement:
CREATE TABLE PLAYERS (PLAYERNO SMALLINT NAME CHAR(15) INITIALS CHAR(3) BIRTH_DATE DATE SEX CHAR(1) JOINED SMALLINT STREET CHAR(15) HOUSENO CHAR(4) POSTCODE CHAR(6) TOWN CHAR(10) PHONENO CHAR(10) LEAGUENO CHAR(4) PRIMARY KEY (PLAYERNO) CHECK (PLAYERNO >= 1) CHECK (SEX IN ('M', 'F')) CHECK (JOINED >= 1970) CHECK (YEAR (BIRTH_DATE) <= NOT NULL, NOT NULL, NOT NULL, , NOT NULL, NOT NULL, NOT NULL, , , NOT NULL, , , , , , , JOINED) );

CREATE TABLE (2)


Example 17.3: A CREATE TABLE statement for table PENALTIES, using referencing actions. Solution (not working in Solid):
CREATE TABLE PENALTIES (PAYMENTNO SMALLINT NOT NULL, PLAYERNO SMALLINT NOT NULL, PAYMENT_DATE DATE NOT NULL, AMOUNT DECIMAL (7,2) NOT NULL, PRIMARY KEY (TEAMNO) , FOREIGN KEY (PLAYERNO) REFERENCES PLAYERS (PLAYERNO) ON UPDATE CASCADE ON DELETE CASCADE , CHECK (PAYMENTNO >= 1) , CHECK (PAYMENT_DATE >= '1980-01-01'), CHECK (AMOUNT >= 0.00) );

ALTER TABLE
Nowadays many SQL products support foreign keys definition in ALTER TABLE statements after all tables have been defined:
CREATE TABLE PENALTIES (PAYMENTNO INTEGER NOT NULL, PLAYERNO SMALLINT NOT NULL, PAYMENT_DATE DATE NOT NULL, AMOUNT DECIMAL(7,2) NOT NULL, PRIMARY KEY (PAYMENTNO) , CHECK (PAYMENTNO >= 1) , CHECK (PAYMENT_DATE >= '1980-01-01'), CHECK (AMOUNT >= 0.00) );

ALTER TABLE PENALTIES ADD FOREIGN KEY (PLAYERNO) REFERENCES PLAYERS (PLAYERNO) ON UPDATE CASCADE ON DELETE CASCADE;

Exercises Tennis Database (1)


Give SQL SELECT statements for answering the following questions: 1. Which players (list the player number) won a match for team 1? 2. List the player number, the name and the league number of all female league players. 3. List player numbers of the chairman, the treasurer and the secretary that are currently in position (i.e. the end date is not entered).

Exercises Tennis Database (2)


Give SQL SELECT statements for answering the following questions:

1.
2.

Give the number of leagueplayers of the tennis club.


Which players won more than one match? List for each of these players the player number and the number of matches won, in descending order of this number. Which players played matches for two different teams?

3.

4. List the player number of all players who won a match for team 1. Additional question: Display also the name of the player. 5. List the numbers of all players who have a total penalty amount of more than $100? Additional question: Display also the name of the player.

Exercises Tennis Database (3)


Give SQL SELECT statements for answering the following questions:

1.
2.

Which players have a total penalty amount of more than 100? List player number, name and the total penalty amount.
List for each penalty incurred by a team captain the payment number, the player number and the player name.

Exercises Tennis Database (4)


Give SQL SELECT statements for answering the following questions:

1.

List match number, player number and score of all matches played for team 2 by a female player. Remark: Use a query with subquery.
List the player number, name, initials of all players who played matches for team 1 and team 2. List the data of the most recent penalty. Give also the name and initials of the player involved. (This one is more advanced.) Can the next query be written as a join?
SELECT FROM WHERE PLAYERNO, NAME, INITIALS PLAYERS PLAYERNO NOT IN ( SELECT PLAYERNO FROM MATCHES WHERE TEAMNO = 1 );

2. 3. 4.

You might also like