Database Operation Examples
This page contains a few examples of database operations. We'll use a truly important topic: roller coasters. Here's a table of of some arbitrarily-selected ones. The speed is the max in MPH; the length and height are in feet.

Coasters:

Coaster_Name Park_Name City State Speed Length Height Loops Construction
Screamin' EagleSix FlagsSt. LouisMO6238721100wood
Mr FreezeSix FlagsSt. LouisMO7013002181steel
The BossSix FlagsSt. LouisMO6650511220wood
Timber WolfWorlds of FunKansas CityMO5342301000wood
BoomerangWorlds of FunKansas CityMO479351163steel
TempestoBusch GardensWilliamsburgVA628631501steel
Rebel YellKing's DominionRichmondVA563368850wood
Georgia CycloneSix FlagsAtlantaGA502970950wood
GoliathSix FlagsAtlantaGA7044802000steel
Great Am Scream MachineSix FlagsAtlantaGA5734501050wood

These data come from the Roller Coaster Database web site.

And, here are a couple more boring tables giving the state names, abbreviations, and populations (2010 census).

States:

Abbrev Name Capital Population
MSMississippiJackson2,967,297
MOMissouriJefferson City5,988,927
NYNew YorkAlbany19,378,102
LALouisianaBaton Rouge4,533,372
VAVirginiaRichmond8,001,024
GAGeorgiaAtlanta9,687,653
MDMarylandAnnapolis5,773,552

States2:

Abbrev Name Capital Population
LALouisianaBaton Rouge4,533,372
MNMinnesotaSt. Paul5,303,925
MDMarylandAnnapolis5,773,552
NMNew MexicoSanta Fe2,059,179

Select

Select is probably the most obvious operation. It chooses rows (records, tuples) from a table. For instance, we can select from Coasters where Construction = 'wood' and length < 3500. This results in the following table:
Coaster_Name Park_Name City State Speed Length Height Loops Construction
Rebel YellKing's DominionRichmondVA563368850wood
Georgia CycloneSix FlagsAtlantaGA502970950wood
Great Am Scream MachineSix FlagsAtlantaGA5734501050wood

Alternatively, we could select from States where Name ends with the letter 'a':

Abbrev Name Capital Population
LALouisianaBaton Rouge4,533,372
VAVirginiaRichmond8,001,024
GAGeorgiaAtlanta9,687,653

Project

The project operation selects columns (attributes) from a table (relation) to make a new one. So we might project Coaster_Name, Speed, and Length from Coasters:
Coaster_Name Speed Length
Screamin' Eagle623872
Mr Freeze701300
The Boss665051
Timber Wolf534230
Boomerang47935
Tempesto62863
Rebel Yell563368
Georgia Cyclone502970
Goliath704480
Great Am Scream Machine573450

We can also combine these operations. For instance, we can select on Coasters where Speed ≥ 60, then project on Coaster_Name. That yields this table:

Coaster_Name
Screamin' Eagle
Mr Freeze
The Boss
Tempesto
Goliath
Which is just a list of the “fast” coasters in the table, where fast means 60 or over.

Union

The union operation just combines the contents of tables. It only makes sense when they have the same structure. Tables are sets of tuples, and this is just the set union operation. For instance, States + States2 is:
Abbrev Name Capital Population
MSMississippiJackson2,967,297
MOMissouriJefferson City5,988,927
NYNew YorkAlbany19,378,102
LALouisianaBaton Rouge4,533,372
VAVirginiaRichmond8,001,024
GAGeorgiaAtlanta9,687,653
MDMarylandAnnapolis5,773,552
MNMinnesotaSt. Paul5,303,925
NMNew MexicoSanta Fe2,059,179
Notice that the Louisiana and Maryland entries only appear once in a union, as is correct for set union.

Difference

The difference operation removes from the first table the tuples which are also in the second. States - States2 is:
Abbrev Name Capital Population
MSMississippiJackson2,967,297
MOMissouriJefferson City5,988,927
NYNew YorkAlbany19,378,102
VAVirginiaRichmond8,001,024
GAGeorgiaAtlanta9,687,653

Join

The join operation is used to combine relations based on a common attribute. Generally, a join is performed on tables with different properties. For instance, we can do a join on Coasters.State = States.Abbrev. That would give us:
Coaster_Name Park_Name City State Speed Length Height Loops ConstructionAbbrev Name Capital Population
Screamin' EagleSix FlagsSt. LouisMO6238721100woodMOMissouriJefferson City5,988,927
Mr FreezeSix FlagsSt. LouisMO7013002181steelMOMissouriJefferson City5,988,927
The BossSix FlagsSt. LouisMO6650511220woodMOMissouriJefferson City5,988,927
Timber WolfWorlds of FunKansas CityMO5342301000woodMOMissouriJefferson City5,988,927
BoomerangWorlds of FunKansas CityMO479351163steelMOMissouriJefferson City5,988,927
TempestoBusch GardensWilliamsburgVA628631501steelVAVirginiaRichmond8,001,024
Rebel YellKing's DominionRichmondVA563368850woodVAVirginiaRichmond8,001,024
Georgia CycloneSix FlagsAtlantaGA502970950woodGAGeorgiaAtlanta9,687,653
GoliathSix FlagsAtlantaGA7044802000steelGAGeorgiaAtlanta9,687,653
Great Am Scream MachineSix FlagsAtlantaGA5734501050woodGAGeorgiaAtlanta9,687,653
This associates with each coaster the data for the state it is located in. In practice, a join is usually followed by a project. Perhaps we might project the Coaster_Name and Population fields to find out the population of the state where each one is located:
Coaster_Name Population
Screamin' Eagle5,988,927
Mr Freeze5,988,927
The Boss5,988,927
Timber Wolf5,988,927
Boomerang5,988,927
Tempesto8,001,024
Rebel Yell8,001,024
Georgia Cyclone9,687,653
Goliath9,687,653
Great Am Scream Machine9,687,653

Perhaps a more interesting example would be to follow the same join with a project on Park_Name, State, and Population. That will give us:

Park_Name State Population
Six FlagsMO5,988,927
Worlds of FunMO5,988,927
Busch GardensVA8,001,024
King's DominionVA8,001,024
Six FlagsGA9,687,653
This result has fewer tuples than the join because some duplicates are discarded. Again, we are treating database tables as sets.

If we're interested knowing which coasters are located in state capitals (there must be some deep sociological significance to this), we could join States and Coasters for Coasters.State = States.Abbrev, then select for Coasters.City = States.Capital. That gives:

Coaster_Name Park_Name City State Speed Length Height Loops ConstructionAbbrev Name Capital Population
Rebel YellKing's DominionRichmondVA563368850woodVAVirginiaRichmond8,001,024
Georgia CycloneSix FlagsAtlantaGA502970950woodGAGeorgiaAtlanta9,687,653
GoliathSix FlagsAtlantaGA7044802000steelGAGeorgiaAtlanta9,687,653
Great Am Scream MachineSix FlagsAtlantaGA5734501050woodGAGeorgiaAtlanta9,687,653
You might then project the interesting fields.

Product

The product operation simply produces all combinations of records from each database. The product of Coasters and States2 contains 10 × 4 = 40 records. Here are some of them:
Coaster_Name Park_Name City State Speed Length Height Loops ConstructionAbbrev Name Capital Population
Screamin' EagleSix FlagsSt. LouisMO6238721100woodLALouisianaBaton Rouge4,533,372
Screamin' EagleSix FlagsSt. LouisMO6238721100woodMNMinnesotaSt. Paul5,303,925
Screamin' EagleSix FlagsSt. LouisMO6238721100woodMDMarylandAnnapolis5,773,552
Screamin' EagleSix FlagsSt. LouisMO6238721100woodNMNew MexicoSanta Fe2,059,179
Mr FreezeSix FlagsSt. LouisMO7013002181steelLALouisianaBaton Rouge4,533,372
Mr FreezeSix FlagsSt. LouisMO7013002181steelMNMinnesotaSt. Paul5,303,925
•  •  •
Great Am Scream MachineSix FlagsAtlantaGA5734501050woodMNMinnesotaSt. Paul5,303,925
Great Am Scream MachineSix FlagsAtlantaGA5734501050woodMDMarylandAnnapolis5,773,552
Great Am Scream MachineSix FlagsAtlantaGA5734501050woodNMNew MexicoSanta Fe2,059,179

The join operation can be represented as a product followed by a select, but join is often listed as an operation in its own right. The SQL select operation can also perform join and project, and is frequently used to do all three at once.