0% found this document useful (0 votes)
9 views

Structured Query Language - Lecture 10

The document provides an overview of SQL including its purpose, basic commands like SELECT and JOIN, and examples of SQL queries. SQL is used to communicate with databases and retrieve or update data. The document explains concepts like table joins, different join types, and how to construct SQL queries to select, filter, and order data.

Uploaded by

thelangastamper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Structured Query Language - Lecture 10

The document provides an overview of SQL including its purpose, basic commands like SELECT and JOIN, and examples of SQL queries. SQL is used to communicate with databases and retrieve or update data. The document explains concepts like table joins, different join types, and how to construct SQL queries to select, filter, and order data.

Uploaded by

thelangastamper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Databases

STRUCTURED QUERY LANGUAGE


Quick Recap of SQL

 SQL stands for Structured Query Language.


 SQL is used to communicate with a database.
 It is the standard language for relational database management systems.
 SQL statements are used to perform tasks such as update data on a database, or retrieve data from a database.
 Some common relational database management systems that use SQL are:
 Oracle, Sybase, Microsoft SQL Server, Access, Ingres, etc.
 Although most database systems use SQL, most of them also have their own additional proprietary extensions
that are usually only used on their system.
 However, the standard SQL commands such as "Select", "Insert", "Update", "Delete", "Create", and
"Drop" can be used to accomplish almost everything that one needs to do with a database.
 We have seen what lies in front of forms and reports and table. So what is syntax is behind these forms
and reports?
A bit More SQL

 Some examples
 General syntax
 Joins in more detail
 More examples
 Summary
SQL Example 1

 Consider this example Access made for us a while back:


SELECT Wines.Name, Wines.[White/Red/Rose], Wines.Type, Wines.Price
FROM Wines
WHERE (((Wines.Price)<200))
ORDER BY Wines.Name;
 What does this actually do?
 SELECT – chooses specific fields
 FROM says from which table
 WHERE selects for particular criteria
 ORDER BY sorts as you request
SQL Example 2

 What does this example do?


SELECT Stock.Description, [InStock]*[CostPrice]
AS [Inventory Value]
FROM Stock
ORDER BY Stock.Description;
 The keyword “AS” allows us to give a name to the new field
 Called an alias
 Convenient for creating a new name without changing the table design
 “Inventory Value” is a calculated field
SQL Example 3

 Here is another that Access created for us:


SELECT Wines.Name, Wines.Type, Wines.[White/Red/Rose], Wineries.Name, Wineries.
[Telephone Number]
FROM Wineries
INNER JOIN Wines ON Wineries.[Winery Code] = Wines.Winery
WHERE (((Wines.[White/Red/Rose]) Like“White"))
ORDER BY Wines.Name;
 What’s new here?
 INNER JOIN – combines rows from two tables
 Here, Wineries and Wines
 Only including those that match a given condition
 Here, Wineries.[Winery Code] = Wines.Winery
Joins

 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 LEFT JOIN (specified with the keywords LEFT


JOIN and ON) joins two tables and fetches all matching rows of
two tables for which the SQL-expression is true
 plus rows from the first table that do not match any row in the
second table.
 SQL LEFT join fetches a complete set of records from table1,
with the matching records (depending on the availability) in
table2. SELECT *
FROM table1
 The result is NULL in the right side when no matching will take LEFT [ OUTER ] JOIN table2
place ON table1.column_name=table2.column_name;
SQL Right Outer Join or Right 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

 Several forms of SELECT including:-


 Only selecting specific fields
 Selecting fields dependent on a condition
 Various kinds of join
 Option of ordering (sorting)
General Notation

 Table names and field names:-


 If the table is not obvious: TableName.FieldName
 If the field name could be confused with other constructs enclose in [ ] Wines.Type – no
confusion over what the field name is Wineries.[Telephone Number] – field name
contains a space so use [ ]
 SQL statements end in semicolon:

;
More SQL

 Other SQL statements include:-


 Creating tables
 Removing tables from a DB
 Modifying an existing table
 Dropping a DB
 There is whole lot more than we can cover in this Introductory course
 MS Access can generate SQL for you
 More sophisticated tools require you create your own
A Few More Examples

 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

 SQL is a powerful language:-


 Can construct queries, new tables, manage a DB at all levels
 We only touched the surface
 MS Access can create SQL for you but more powerful tools may require you create your own
 Another popular database engine: MySQL
 Free, supported by many users and companies
 Widely used in Internet services
 LAMP: Linux, Apache, MySQL, and PHP
 Linux: a free operating system
 Apache: a free web server
 MySQL: a free database engine
 PHP: a popular programming language for web apps

You might also like