You are on page 1of 10

ITN200: Database Systems

Basic SQL Retrieval

ITN200: Database Systems

Basic SQL Retrieval

Basic SQL Retrieval We use three basic mechanisms for retrieving data. Extract the contents of a table by specifying the rows we are seeking and/or the columns we are interested in. Summarise a table by using statistical functions. Subdivide a table and summarise each subdivision by forming the rows of the table into groups and processing these groups. We control the order in which results are returned. We Are Here

Mechanism 1: Selection and Projection

Querying a database table can be thought of asking for some smaller portion of that table. There are two obvious ways by which we might prune a table by row and by column. DeptName Bread Cake Finance Boss Bruce Cathy Phil Owes 1000 1500 60000 Selection

One way of pruning a table is to view the table as a set of rows and to seek only certain rows. We place restrictions on the values of the columns of the table. This is known as selection.

Our simple facts have been embedded in database tables. We extract and examine these facts using SQL.
sqlbasic-1 sqlbasic-2

Information Modelling

Q1

Information Modelling

Basic SQL Retrieval

Example 1: Give details of departments that owe less than $2000. Select * From Depts d Where d.Owes < 2000 DeptName Bread Cake Boss Bruce Cathy Owes 1000 1500

Example 2: Which makes can Cathy take out?

Select * From ............... Where ...............

Every row in the Depts table is examined. We use the from clause to identify the table or tables that we want to query. We use the where clause to specify the rows that are to be selected. The d in the expression From Depts d is said to be an alias. When we say select *, we are asking for every column in the table.

Here is the basic outline you should use. Now ask what table is involved. ................................................ Example 3: Which people are located in the Finance Dept?

Select * From ............... Where ...............

Here is the basic outline you should use. Now ask what table is involved. Next, what lter is necessary? ................................................
sqlbasic-3 sqlbasic-4

Information Modelling

Basic SQL Retrieval

Information Modelling

Q6

Example 4: Which departments owe an amount in the range $1000 to $5000?

Example 6: Which cars are in use at the moment? Select * From Cars c Where c.OutTo is not null CarNr c1 c2 c6 c8 Make Ford Ford BMW Benz Cost 50 80 250 350 OutTo Bob Cathy Frank Phil OutOn 17-Feb-2005 17-Feb-2005 04-Feb-2005 10-Dec-2004

Select * From Depts d Where ...............

We need to look in the Depts table. Sounds like two conditions are involved? How do we check the low end? ................................................ Example 5: Which people can borrow either a Benz or a BMW?

The condition c.OutTo is not null allows us to detect whether there is a value in a columns in which nulls might appear. We could write c.OutTo is null if we wanted to identify cars that were not in use.

Select * From Cantake c Where ...............

This time we look in the Cantake table. Again, it sounds like two conditions are required. How do we require that person can take a BMW? ................................................
sqlbasic-5 sqlbasic-6

ITN200: Database Systems

Basic SQL Retrieval

Information Modelling

Q9

Projection As was said earlier, querying a database table can be thought of asking for some smaller portion of that table. A second way of pruning a table is to view the table as a set of columns and to seek only certain columns. This is known as projection. Example 7: Give the names and heads of all departments. Select d.DeptName, d.Boss From Depts d DeptName Bread Cake Finance Boss Bruce Cathy Phil

Example 9: We may rename a column. Suppose we want to see what a department owes now and in the future. Select d.DeptName, d.Owes, d.Owes*1.1 as Future From Depts d DeptName Bread Cake Finance Owes 1000 1500 60000 Future 1100 1650 66000

Example 8: We may ask for calculations to be performed. If each department has its liability increased by 10%, what will each owe? Select d.DeptName, d.Owes, d.Owes*1.1 From Depts d DeptName Bread Cake Finance Owes 1000 1500 60000 Owes*1.1 1100 1650 66000
sqlbasic-7 sqlbasic-8

Information Modelling

Q10

ITN200: Database Systems

Basic SQL Retrieval

Example 10: Name those employees who can borrow either a Benz or a BMW Select distinct c.Name From Cantake c Where c.Make = BMW or c.Make=Benz Name Bruce Cathy Frank Phil Phil can borrow both makes. A simple scan through the Cantake table is likely to result in his name appearing twice. We use the word distinct to indicate that we want duplicate results rows to be compressed into a single row. What is the signicance of the order in which the names appear?

Mechanism 2: Statistical Queries

Example 11: What are the total and average amounts owed by all departments? Select sum(d.Owes), avg(d.Owes) From Depts d sum(Owes) 62500 avg(Owes) 20833.3333

Example 12: What are the least and most amounts owed by any of the departments? Select min(d.Owes), max(d.Owes) From Depts d min(Owes) 1000 max(Owes) 60000

The max and min functions will also work on alphanumeric columns. For example, in the Employees table, min(Name) = Betty and max(Name) = Phil.

sqlbasic-9

sqlbasic-10

Information Modelling

Q13

ITN200: Database Systems

Basic SQL Retrieval

Example 13: How many cars does the company own? Select count(*) From Cars c Count(CarNr) 10 Example 14: How many cars are in use at the moment? Select count(*) From Cars c Where c.OutTo is not null count(*) 4

Mechanism 3: Group by Queries Example 15: For each person who can use a car, state the number of dierent makes they can take out. Select c.Name, count(*) From Cantake c Group by c.Name Name Betty Bob Bruce Carol Cathy Frank Fred Phil count(*) 2 1 1 2 4 1 2 2

We need to imagine an intermediate process: Cantake table is sorted into Name order. The resulting table is partioned into groups in the manner shown on the left. SQL then reports once on each group. In this example, it displays the employees name and the number of rows in the group associated with that person. There are limitations on the select clause. Whatever we choose must be single-valued for each group. In this example, the employee name is unique for each group, and so is the groups size.
sqlbasic-11 sqlbasic-12

ITN200: Database Systems

Basic SQL Retrieval

Information Modelling

Q17

Group By Pattern A typical group by query has the following appearance, with the group by clause appearing after the from and where clauses: Select Column, stat-function(...) From Table Group by Column The table is categorised according to the column named in the group clause. The select clause is then used to identify the category, and to summarise that category, in some way. Example 16: How many people can take out each make of car? Select c.Make, count(*) From Cantake c Group by c.Make Name BMW Benz Ford Honda Mazda count(*) 4 1 5 3 2

Example 17: What is the average, minimum and maximum cost for each make of car? Select c.Make, avg(c.Cost), min(c.Cost), max(c.Cost) From Cars c Group by c.Make Make BMW Benz Ford Honda Mazda CarNr c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 avg(Cost) 183.333333 300 93.3333333 150 80 Make Ford Ford Mazda BMW BMW BMW Benz Benz Ford Honda min(Cost) 100 250 50 150 80 OutTo Bob Cathy ? ? ? Frank ? Phil ? ? Max(Cost) 250 350 150 150 80 OutOn 17-Feb-2005 17-Feb-2005 ? ? ? 04-Feb-2005 ? 10-Dec-2004 ? ?

Cost 50 80 80 100 200 250 250 350 150 150

sqlbasic-13

sqlbasic-14

ITN200: Database Systems

Basic SQL Retrieval

Information Modelling

Basic SQL Retrieval

The Having Clause This allows us to control which groups are reported. The having clause applies to groups, in the same way that the where clause applies to individual rows. It immediately follows the group clause. The conditions must either target the category determined by the group, or be statistical in nature. A having clause can appear without a group by, in which case, the entire table is treated as a single group. Example 18: Which makes cost more than $150, on average? Select Make, avg(c.Cost) From Cars c Group by c.Make Having avg(c.Cost) > 150 Make BMW Benz avg(Cost) 183.333333 300

Example 19: For employees who can take out more than one make of car, show how many makes per employee.

Select ............... From Cantake Group by ............... Having ...............

The table involved is Cantake. What are we going to group by? What group-lter is necessary? ................................................

sqlbasic-15

sqlbasic-16

Information Modelling

Basic SQL Retrieval

ITN200: Database Systems

Basic SQL Retrieval

Example 20: Are there any makes that more than two people can take?

The Order by Clause The order by clause allows the nal results to be displayed in a given sequence.

Select ............... From Cantake Group by ............... Having ...............

Example 21: List all the cars in order of make, and within that, by descending order of cost. Select Make, Cost, CarNr From Cars Order by Make, Cost desc Make BMW BMW BMW Benz Benz Ford Ford Ford Honda Mazda Cost 250 200 100 350 250 150 80 50 150 80 CarNr 6 5 4 8 7 9 2 1 10 3

The table involved is Cantake. What are we going to group by? Next, what lter is necessary? ................................................

The major sequence is Make. First and foremost, the list is produced in order of make. The minor sequence is Cost desc. Cars of the same make are sorted according to their cost. The keyword desc is used to specify that, for cars of the same make, the most expensive are to appear rst, followed by the less expensive.
sqlbasic-17 sqlbasic-18

ITN200: Database Systems

Basic SQL Retrieval

ITN200: Database Systems

Basic SQL Retrieval

Comments

The SQL SELECT Statement Basic Form:

Ordering is the very last action that SQL takes in answering a query. You can order the results by some column, even if that column does not appear in the results. Select CarNr From Cars Order by Cost The default order is ascending. You can refer to a result column either by name or by position. Select CarNr, Cost From Cars Order by 2 means by order of cost (the second column in the select clause).

Select From Where Group by Having Order by

list of expressions list of tables condition list of expressions condition list of order-expressions

Additional Clauses:

Basic SQL Retrieval: now you can

Use three basic retrieval mechanisms to: Extract the contents of a table by means of the where and the select clauses. Summarise a table by means of statistical functions such as count, sum, avg, max and min. Subdivide a table and summarise each subdivision by means of the group by and having clauses. Control the order in which results are returned.

sqlbasic-19

sqlbasic-20

You might also like