You are on page 1of 8

JOINING TABLES

Vidya kumari
Joining Tables

• So far, all the queries you’ve seen have been concentrated on a single
table.
• But SQL also allows you to query two or more tables at a time, and display a
combined result set.
• This is technically referred to as a join, since it involves “joining” different
tables at common fields (the foreign keys) to create new views of the data.
Here’s an example of joining the songs and artists tables together using the
common artist_id field (the WHERE clause is used to map the common fields
to each other):
mysql> SELECT song_id, song_title, artist_name FROM songs, artists
-> WHERE songs.fk_song_artist = artists.artist_id;
+---------+---------------------------+-------------+
| song_id | song_title | artist_name |
+---------+---------------------------+-------------+
| 1 | Janie's Got A Gun | Aerosmith |
| 2 | Crazy | Aerosmith |
| 8 | Gimme Gimme Gimme | Abba |
| 9 | SOS | Abba |
| 10 | Dancing Queen | Abba |
| 11 | Voulez Vous | Abba |
| 7 | Apologize | Timbaland |
| 4 | Sure | Take That |Chapter 7: Working with Databases and SQL 199
| 5 | Pray | Take That |
| 6 | Another Crack In My Heart | Take That |
| 12 | Babe | Take That |
| 3 | En Las Delicious | Cubanismo |
+---------+---------------------------+-------------+
12 rows in set (0.00 sec)
And here’s an example of joining all three tables together and then filtering the
result set even further to include only those songs with a rating of 4 or higher and
with non-U.S. artists:

mysql> SELECT song_title, artist_name, rating_name


-> FROM songs, artists, ratings
-> WHERE songs.fk_song_artist = artists.artist_id
-> AND songs.fk_song_rating = ratings.rating_id
-> AND ratings.rating_id >= 4
-> AND artists.artist_country != 'US’;

+------------------+-------------+-------------+
| song_title | artist_name | rating_name |
+------------------+-------------+-------------+
| En Las Delicious | Cubanismo | Excellent |
| Pray | Take That | Good |
| SOS | Abba | Good |
| Dancing Queen | Abba | Good |
+------------------+-------------+-------------+
4 rows in set (0.02 sec)
Modifying and Removing Records
Just as you INSERT records into a table, so too can you delete records with
the DELETE statement.
Typically, you would select a specific subset of rows to be deleted by adding
the WHERE clause to the DELETE statement, as in the next example, which
deletes all those songs with a rating less than or equal to 3:
mysql> DELETE FROM songs
-> WHERE fk_song_rating <= 3;
Query OK, 5 rows affected (0.02 sec)
There’s also an UPDATE statement, which can be used to change the
contents of a record; this too accepts a WHERE clause, so that you can apply
changes to only those records matching specific criteria.
Consider the following example, which changes the rating 'Excellent' to
'Fantastic’:

mysql> UPDATE ratings SET rating_name = 'Fantastic'


-> WHERE rating_name = 'Excellent’;

Query OK, 1 row affected (0.00 sec)


Rows matched: 1 Changed: 1 Warnings: 0
You can alter multiple fields by separating them with commas.
Here’s an example that updates a particular song record in the songs table,
changing both its title and its rating:

mysql> UPDATE songs SET song_title = 'Waterloo',


-> fk_song_rating = 5
-> WHERE song_id = 9;

Query OK, 1 row affected (0.00 sec)


Rows matched: 1 Changed: 1 Warnings: 0

You might also like