Membership
Main Menu
Forum Boards
Stats
- 18 tutorials
- 72,311 members
- 696,197 forum posts
- 11 blog posts
Tutorials
Data Joins Unions
Views: 22008
Introduction
In this tutorial we look at using joins and unions in our database queries. Before we get to those, however, we'll first put our data into a format where we can take full advantage of the power of SQL joins. This process of removing redundant duplication and repetition of data is called "normalization". I am not covering it in any detail here but I will give you a rough-and-ready method of handling data relationships. (Google "data normalization" so you know exactly what you are doing. In practice you'll use first, second and third normal forms. Fourth and beyond are reserved for theoretical classroom exercises.)
This tutorial is primarily written for MySQL. Some adjustment to the queries may be required for other dialects of SQL.
The Scenario
Throughout this tutorial we will use a fictitious school database. The school's headmaster, Mr Beetsem-Daley, has commissioned us to build an intranet for pupil administration. He requires us to record which pupils are in each house and the subjects chosen for study by each pupil. His data is currently held in spreadsheet and looks like this:
+------------------+------------+------------------+----------------------------------------+ | pupil_name | house_name | house_master | chosen subjects | +------------------+------------+------------------+----------------------------------------+ | Adam Simms | Laker | Frank Morrisson | Economics, English, German | | Allan Blair | Grace | Charles Hadleigh | Economics, Geography, German, Physics | | Anna Hamilton | Grace | Charles Hadleigh | English, History | | Anne Bailey | Jardine | Pauline Fforbes | English, German, History, Physics | | Anthony Bell | Cowdrey | Robert Bingley | Economics, Geography, History, Physics | | Caroline Freeman | Grace | Charles Hadleigh | Biology, Chemistry, Geography, History | | David Powell | Laker | Frank Morrisson | Economics, Maths, Physics | | Emma Watson | Cowdrey | Robert Bingley | English, Geography | | Gearge Wilson | Laker | Frank Morrisson | Chemistry, Economics, English, History | | Henry Irving | Cowdrey | Robert Bingley | Biology, Chemistry, Maths | | Jane Morrison | Laker | Frank Morrisson | Economics, English, Physics | | John Patterson | Jardine | Pauline Fforbes | Geography, German | | John Tully | Jardine | Pauline Fforbes | Biology, English, History,Maths | | John Watson | Grace | Charles Hadleigh | Chemistry, Economics, English, German | | John Williams | Grace | Charles Hadleigh | English, German, History | | Margaret Norton | Cowdrey | Robert Bingley | German, Physics | | Mary Blake | Cowdrey | Robert Bingley | German, History, Physics | | Mary Sheldon | Jardine | Pauline Fforbes | Chemistry, Economics, English, History | | Mary Whitehouse | Grace | Charles Hadleigh | English, Geography, History, Maths | | Michael Grove | Jardine | Pauline Fforbes | Economics, English, Physics | | Peter Adamson | Laker | Frank Morrisson | Chemistry, Economics, German, Physics | | Peter Appleby | Jardine | Pauline Fforbes | Biology, Economics, German, Maths | | Wayne Jones | Laker | Frank Morrisson | Biology, Economics, German, Maths | | William Smith | Cowdrey | Robert Bingley | Biology, Economics, Maths, Physics | +------------------+------------+------------------+----------------------------------------+
Instantly we notice the repetition of the house and house-master names and the totally unsuitable comma-separated lists.
