07 SQL2 Select
07 SQL2 Select
ICT - 209
Database Applications
MySQL 2
SELECT
1
Getting Data From a Database with MySQL
• The SQL SELECT statement is used to retrieve data from one or more
tables in a database
• It allows you to choose which row and which columns you would like
to retrieve and allows joins across several tables.
http://dev.mysql.com/doc/refman/5.0/en/select.html
2
Example Tables for This Lecture
• Books Borrowers
Name Number Name Number Dept
Book1 1 Anne 1 Maths
Book2 2 Bill 2 Maths
Book3 3 Claire 3 French
Book4 4 Duncan 4 French
Book5 5 Edward 5 French
• Loans
BookNumber PersonNumber
1 2
3 4
3
SELECT From One Table
4
SELECT and Calculations
• There are many functions that you can include in a SELECT statement
• Some simple ones are:
or even
• SELECT sin(45)
Calculates the sine of 45 – no need to reference a table at all
5
Joining Tables
• SELECT borrowers.Name, books.Name FROM borrowers, books
6
Looking up a Foreign Key
• Let’s say we want to know who has borrowed Book1
• The Loans table has this information, but not in a form that is immediately
accessible:
BookNumber PersonNumber
1 2
3 4
• SELECT Number FROM Books WHERE Name=“Book1”
Tells us that book1 has the ID number 1
7
Put it all Together
• SELECT Borrowers.Name FROM Borrowers, Books, Loans
WHERE Books.name="Book1" AND Books.Number=loans.booknumber
AND Borrowers.number=loans.personnumber
• Returns Bill.
• Note the use of AND. You can also use OR and NOT. Other useful logic:
– >, <, <=, >=, =, != (or <>)
– When comparing NULL, use IS NULL or IS NOT NULL
– You can search for partial strings using LIKE:
The _ is a single character wild card, so above looks for names with two characters
ending in e
8
Comparison Examples
• SELECT * FROM table WHERE x BETWEEN 1 and 3
• Note: Commands and Names (such as table or field names) are not
case sensitive:
– Select = SELECT
– Table = table
• String literals may be case sensitive, depending on the collation.
– _ci at the end of the collation name means case insensitive
– Force a case sensitive search with
SELECT * FROM table WHERE BINARY name="John"
9
How to Construct a SELECT
• Decide what columns you want in your results table:
• Then list all the tables involved in the selection (some won’t actually
provide a field in the select list, but are involved anyway
• Then follow the path from what you want to look up to the answer,
building x=y statements joined by AND (or OR).
10
More General Joins
• If we want to list all the people who have a book, and the books they
have:
• Note that
• SELECT * FROM Borrowers, Books, Loans WHERE
Books.Number=loans.booknumber AND
Borrowers.number=loans.personnumber
• Is an Equijoin, and lists all the columns from all tables that meet the
selection criteria, including them once for each table they appear in.
This is not very useful, but shows that everything matches up.
11
Inner and Outer Joins
• We have just seen an inner join. Only rows that meet the criteria
completely are returned.
• Now we look at outer joins – where more data is returned about one or
more of the fields.
• Lets say we want to list all of the people in the borrowers database,
regardless of whether or not they have a book out. If they do have a
book, we want to list that too, otherwise we will just return NULL for
people with no current book
http://dev.mysql.com/doc/refman/5.0/en/join.html
12
Join Syntax
• SELECT Borrowers.name, Books.name FROM Borrowers LEFT OUTER JOIN
(Books, Loans) ON (Books.Number=loans.booknumber AND
Borrowers.number=loans.personnumber)
• Note the syntax SELECT cols FROM table LEFT OUTER JOIN
(table,table) ON (conditions)
14
Groups
• We may want to summarise the data in the database, and there are
some statistical functions available.
• We have already seen MAX and MIN and there are others:
– AVG
– COUNT
• For example, how many people are there from each department?
15
How Many Books Does Each Department Have?
• We could show the list and count them ourselves:
16
Another Example
• SELECT Dept, COUNT(Dept) FROM Borrowers, Books, Loans
WHERE Books.Number=loans.booknumber
AND Borrowers.number=loans.personnumber GROUP BY Dept HAVING
COUNT(Dept) = (SELECT MAX(COUNT(Dept)))
• Selects the department with the most books out and reports how many
they have
17
Union
• Allows you to make more than one selection at the same time and put
the results together
• A bit like using OR, but more flexible:
• Is the same as
18
Union
• But it is useful when selecting
across more than one table
Staff
• SELECT Name FROM Borrowers
UNION Name Number Role
SELECT Name FROM Staff Anne 1 Manager
• UNION ALL
19
Selection Manipulation
• Here are a few things you can do to the selected list:
• SELECT DISTINCT …
20
More on Sub Queries
• We can use a sub query, as we have already seen, but what happens
when the sub query produces more than 1 row?
• SELECT * FROM staff WHERE Name = ANY (SELECT Name FROM Borrowers)
• Selects staff whose name appears in both Staff and Borrowers tables
SELECT * FROM borrowers WHERE Number > ALL (SELECT Number FROM staff)
• Selects those borrowers who have a higher number than ALL of the
staff
21
Aliases
• You can give an alias to a table or column to make it easier to refer to
later in a query or to make it easier to read in the output:
22
Aliases
• SELECT Role, COUNT(Role) FROM staff GROUP BY role
HAVING COUNT(role) > 1
Can be re-written as
• We have made aliases for the tables to make reference to them easier
23