Structured Query Language - Lecture 10
Structured Query Language - Lecture 10
Some examples
General syntax
Joins in more detail
More examples
Summary
SQL Example 1
Create a new table (possibly just for a query or report) by combining 2 or more other tables
Needs a relation (linking primary and foreign keys) in most interesting cases
The more you normalize, the more joins you need
In DB query design, the order of joins can make a big difference to time taken for a query
Types of Join
SQL CROSS JOIN produces a result set which is the number of rows
in the first table multiplied by the number of rows in the second table
If no WHERE clause is used along with CROSS JOIN.
This kind of result is called as Cartesian Product.
If WHERE clause is used with CROSS JOIN, it functions like an INNER
JOIN.
Cross join – Create a table with every combination of two given tables
e.g.
SELECT *
SELECT *
FROM Wineries CROSS JOIN Wines; FROM table1
This would create a huge table with a row for every possible combination CROSS JOIN table2;
of winery and wine (here, “*” means all fields)
SQL Inner Join
SQL INNER JOIN selects all rows from both participating tables as
long as there is a match between the columns.
The INNER JOIN in SQL joins two tables according to the matching
of a certain criteria using a comparison operator
An SQL INNER JOIN is same as JOIN clause, combining rows from
two or more tables.
Similar to a cross join except it only keeps rows where a key
matches between the two tables
SELECT *
FROM Wineries INNER JOIN Wines SELECT *
FROM table1 INNER JOIN table2
ON Wineries.[Winery Code] = Wines.Winery; ON table1.column_name = table2.column_name;
SQL Left Outer Join or Left Join
The SQL RIGHT JOIN, joins two tables and fetches rows based
on a condition, which is matching in both the tables
before and after the JOIN clause mentioned in the syntax below
The unmatched rows will also be available from the table written
after the JOIN clause ( mentioned in the syntax below ).
SQL RIGHT join fetches a complete set of records from
table2, i.e. the rightmost table after JOIN clause, with the
matching records (depending on the availability) in table1.
SELECT *
The result is NULL in the left side when no matching will take place.
FROM table1
RIGHT [ OUTER ] JOIN table2
ON table1.column_name=table2.column_name;
What Types of Join does Access Support?
More on SELECT
;
More SQL
When you create a query or report in Access, you can inspect the SQL
Useful for learning SQL and creating your own examples
You can edit the generated SQL
Can be quicker and easier than using the user interface
A few more examples to illustrate what you can do
Further Examples (1)
What if we want to see what the total profit is per item in a store database?
SELECT Stock.Description, ([InStock]*([SellingPrice]- [CostPrice])) AS [Total Profits]
FROM Stock
ORDER BY Stock.Description;
What does this do?
To calculate using fields we put the field name in square brackets
The AS keyword gives a name to the calculated field
It is the result of a query, not stored as a field in the DB
Further Examples (2)
What if we want to see what the profit is per item in the store database?
SELECT Stock.Description, [SellingPrice]-[CostPrice] AS [Profit per item]
FROM Stock
ORDER BY [SellingPrice]-[CostPrice];
What does this add that’s new?
We can sort by a calculated value
Further Examples (3)
What if we want to ask the user which supplier’s stock they want to see?
SELECT Stock.ItemId, Stock.Description, Stock.InStock
FROM Suppliers
INNER JOIN Stock ON Suppliers.SCode = Stock.SupplierCode
WHERE (((Suppliers.SName)=[please enter supplier’s name]));
What does this add that’s new?
The text in the last square brackets is not a field name so it is interpreted as a question and so the user is prompted for a value
The result of the query will only be the fields selected where the supplier name matches what the user types in
What you Need to Know?
Access can sometimes be more aggressive than it needs to be in wrapping field names in [ ]
it isn’t wrong to do this so use this if not sure
Table names can also be enclosed in [ ] if necessary but this is not needed in most of our examples
SQL is a huge language with variations on different platforms:
Do not try to memorize all the details
Know the basic syntaxes of SQL and what each of the key operative words do
SELECT, FROM, WHERE, JOINS, ORDER BY
Be able to read a given example
Use it to construct a similar example with small differences
For example change this to sort the result by cost price:
SELECT Stock.Description, [InStock]*[CostPrice] AS [Inventory Value]
FROM Stock
ORDER BY Stock.Description;
SQL Summary