Handout 3 - Introduction To SQL
Handout 3 - Introduction To SQL
doc
Haramaya University
College of Computing and Informatics
RECOMMENDED TEXTS:
Some of the basic T-SQL data types are shown in the table below. Every column in a table
must have a data type. The other DBMSs we have mentioned in class also have these data
types, although some may vary in terms of the number of characters allowed and the storage
space.
The storage size refers to the amount of disk space required to store the value. Whilst this
may not be important when working with small databases, space may be an issue when
working with larger databases.
The data type of a column affects how the column is treated in SQL queries e.g. comparison
values for a character type should be in string delimiters, numerical types do not need
delimiters.
Page 1 of 9
CCI – Computer Science Dept 655445328.doc
2 Naming of Objects
Tables in SQL are referred to by the table name.
The name is usually also qualified with the owner of the table – this is the SQL Server user
that created the table. The ‘dbo’ user owns objects created by the database administrator. The
db owner usually creates objects.
Therefore the fully qualified name for a table named ‘Titles’ would be:
dbo.Titles
The name can be further qualified by putting the database name in front of it. This is to
avoid confusion if tables of the same name exist in different databases. For example:
Pubs.dbo.Titles
Pubs..Titles
(note the two dots, to indicate that the owner name is omitted).
Columns in a table are referred to by prefixing them with the table name. For example:
Titles.Title_ID
Titles.type
All other objects in a database are named (views, indexes, stored procedures etc) and can be
referred to by name, in the same way as tables i.e. owner.object_name.
3 Using NULLs
In a relational database, it is possible to specify that a column can contain a NULL value.
If a column contains the value NULL, it means that the value is unknown or could not be
determined. This is different to a blank or zero value.
Using NULLs means that you can distinguish between entries that represent a deliberately
placed zero or where either the data is not recorded (blanks) and where a NULL has been
explicitly entered.
For example, in the pubs database, a NULL in the Titles.Price column does not mean that the
book is free or has no price. It means that the price is not known or has not yet been
determined.
Page 2 of 9
CCI – Computer Science Dept 655445328.doc
NULL values cannot be used in a column that is used to distinguish one row from another –
i.e. in primary and foreign keys. When creating a table with SQL, it is necessary to specify if
each column allows NULLs or not.
When carrying out comparisons, NULL values in different tables are not seen as being equal
to each other.
The main pane in Query Analyzer is the Editor Pane, in which you type your queries.
The Object Browser to the left of the Editor Pane (if you cannot see it, choose Tools ->
Object Browser from the menus) shows you all the objects in each database on the server.
For example, to select all the records from the publishers table:
- Type in 'select * from publishers', in the Editor Pane
- Hit F5
- the results of the query are displayed in the Results Pane, below the Editor Pane
Page 3 of 9
CCI – Computer Science Dept 655445328.doc
(see Figure 1 below – this shows the Query Analyzer – the query is typed into the Editor
Pane, the resulting data appears in the Results Pane).
Note: In these handouts, we will use the same syntax conventions as in the SQL Server
Books Online. See Appendix A – Syntax Conventions for T-SQL' at the end of this handout
for a description of the syntax conventions (taken from Books Online).
Page 4 of 9
CCI – Computer Science Dept 655445328.doc
SELECT select_list
FROM table_source
[ WHERE search_condition ]
[ ORDER BY order_expression [ ASC | DESC ] ]
[Note: SQL is not case-sensitive; however, it is good practice to use upper-case or proper
case – where first letters are capitalised – for key words such as SELECT, as it makes your
SQL easier to read.]
Example – this query selects Title_ID, Title and Type from the Title table (the projection)
where the Type is “Business” (the selection) and it orders the results by Title:
- the asterisk (*) can be used to select all the columns from a table
Page 5 of 9
CCI – Computer Science Dept 655445328.doc
The most basic type of join retrieves the cross product (or Cartesian product) of all the data in
two tables. For each record in the first table, all the records in the second table are joined.
This creates a very big record set (the number of rows = number of rows in table A x number
of rows in table B).
This is not a particularly useful join, so it is rarely used.
Example:
This cross join would result in a row for every Title-Publisher combination (regardless of
matching columns or keys):
This query is the same (the CROSS JOIN keyword does not have to be specified):
An inner join is used to retrieve rows from the tables where there are corresponding rows,
based on matching values in specified columns in each of the tables.
This is equivalent to choosing to discard unmatched rows from a cross-product – by adding
join criteria to the query.
Suppose we want to find the names of the publishers for the titles we selected above
('business' titles). This is an inner join between titles and pubs, based on the values in the key
field pub_id being equal.
Example:
SELECT title_id, title, type , p.pub_name
FROM titles AS t INNER JOIN publishers AS p ON t.pub_id = p.pub_id
We can also add the search criteria and the order by clause onto this query, if desired:
SELECT title_id, title, type , p.pub_name
FROM titles AS t INNER JOIN publishers AS p ON t.pub_id = p.pub_id
WHERE t.type = 'business'
ORDER BY t.title
Page 6 of 9
CCI – Computer Science Dept 655445328.doc
An outer join retrieves data from the tables where there are corresponding rows, but also
includes rows from one side of the join that have no matching records in the other table in the
join.
A left outer join includes all rows from the table on the left side of the join, regardless of
whether there are matching records in the other table. Columns from the right table where
there is no matching record return NULL values.
Suppose we want to find the Sales for all Titles, including those that do not have any Sales.
As it is possible that there are no Sales for a Title, this is a left outer join with Titles being on
the left side of the join.
Example:
SELECT t.title_id, t.title, s.ord_num, qty
FROM titles t LEFT OUTER JOIN sales s ON t.title_id = s.title_id
A right outer join includes all rows from the table on the right side of the join, regardless of
whether there are matching records in the other table. Columns from the left table where there
is no matching record return NULL values.
If we take the same query as above i.e. to find the Sales for all Titles, including those that do
not have any Sales, but place the Titles table on the right hand side of the join, then it is a
right outer join.
Example:
SELECT t.title_id, t.title, s.ord_num, qty
FROM sales s RIGHT OUTER JOIN titles t ON t.title_id = s.title_id
A full outer join includes all rows from both tables in the join, regardless of matching records
in the other table. The result set shows NULL values where there are no matching records.
Consider Authors and Publishers – both have a City attribute. A LEFT OUTER JOIN on City
would produce all Authors who are in the same City as a Publisher, and Authors who do not
live in the same City as a Publisher.
A RIGHT OUTER JOIN would produce all Publishers who are in the same City as an
Author, and Publishers who do not live in the same City as an Author.
Page 7 of 9
CCI – Computer Science Dept 655445328.doc
Alternatively, an INNER JOIN would produce Authors who live in the same City as a
Publisher. If we wish to see all Authors and Publishers, including those who have a City in
common and those who do not, we can do a FULL OUTER JOIN:
There is an alternative syntax for joins, used by non-ANSI standard implementations of SQL
Server. It is also possible to use this syntax in SQL Server. This style of join is called theta
style. In this style, the join is specified in the WHERE clause, using =, *= or =* operators.
The non-ANSI syntax for each join is shown below.
Inner Join
This syntax is often used instead of the ANSI syntax and is still supported by SQL Server.
It is possible to include multiple tables and multiple columns in all of the join types.
All of the above joins are based on matching values in one column. A join can be on more
than one column.
For example, to retrieve all Authors who live in the same City and State as a Publisher:
Page 8 of 9
CCI – Computer Science Dept 655445328.doc
More than two tables can be joined also – because the result set from a join between two
tables is effectively a table itself.
For example, if we want to get a list of Titles and Authors, we have to join the Titles, Authors
and TitleAuthor tables (remember the many-to-many relationship!):
Page 9 of 9