Practical #2: SQL Queries - “We Know the Song”
Practical Setup
Retrieving the Data
In a terminal, type the following command:
curl http://www.dequidt.me/uploads/gba/chansons.sql -o chansons.sql
Open the file with a text editor, such as kate or gedit (avoid libreoffice or
openoffice if possible).
Conceptual Schema
Reconstruct a conceptual schema from the chansons.sql file, which defines the creation
of relations and the insertion of data for our database.
You can draw your schema on paper or using the Modelio software:
modelio
Database Initialisation
Next we will build a songs database using the chansons.sql file. From the terminal opened
previously (i.e. for retrieving the data or launching modelio), type the following
commands, replacing yourLogin with your Polytech Lille login (e.g. jdequidt); the
default password is postgres:
export PGHOST=serveur-etu.polytech-lille.fr
export PAGER='less -SFMX'
createdb -U yourLogin yourLoginSongs
Then connect to your songs database on the student server:
psql -U yourLogin yourLoginSongs
You can now create the relations (i.e. the tables) and insert the data using our SQL file:
\i chansons.sql
Finally you can list the relations with the \d command and list all databases on the
server with \l.
SQL Queries
Difficulty: RxYour database is ready; you can now interact with your data. For example, to list all
tuples in the CHANSON table, use the following query:
SELECT * FROM chanson;
For the following questions, it is recommended to write your queries in a file (opened with
kateorgedit) saved insqlformat (e.g.solutionSongs.sql) and to copy-paste the queries into the terminal as you go.
- Songs whose title starts with
m. - The label of category 2.
- The category numbers of the albums present in the database. (Avoid duplicates.)
- The number of distinct categories that the albums in the database belong to. Give a meaningful label to the result column.
- The list of people whose surname and first name are both known. Display the result in a
single column containing surname and first name. Use the string concatenation operator
||. Display everything in uppercase (using theupperoperator). Find a meaningful column label. - The list of Springsteen albums, with the number of songs on each album.
- The list of albums that have more than 3 songs.
- The list of performers on the album
sol en si. - The members of the group Téléphone.
- The list of songs with the number of their performers.
- The titles and years of Souchon’s most recent songs (i.e. relative to all of Souchon’s songs).
- The list of songs released before 1995 whose title contains
la. - The list of pairs of singers. Avoid identical pairs and symmetric pairs (e.g. (chamfort, souchon) and (souchon, chamfort)).
- The list of pairs of distinct names not known as groups. Avoid symmetries.
- The list of albums whose title is the same as one of their songs.
Optional Queries
Difficulty: HardWe strongly encourage you to continue with the following SQL queries to improve your
skills:
- Songs whose title contains
mens. - Titles of albums classified in the
rockcategory. - Titles of songs released before 1990.
- Titles of albums released between 1990 and 1995, sorted in alphabetical order.
- Number of albums per category.
- Average number of songs per album.
- The list of rock performers.
- The number of songs performed by Alain Souchon.
- Titles and years of songs performed by Bruce Springsteen.
- The list of names of singers who are not performers.
- Titles and years of the most recent songs (i.e. from the most recent year).
- Use the UNION operator to display the list of rock or rap performers.
- Use a query with a subquery to obtain the names that are not performers (this time we want the names, not just the references).
- The list of albums whose year is earlier than the years of all albums performed by
springsteen. - The list of albums whose year is earlier than at least one album performed by
springsteen.