2015‐2016
Phil Smith
Today Learning outcomes – LO3 Queries and Reporting.
DDL, DML, DCL, TCL, DQL
More DQL – which stands for?
Data Query Language
Basically the Select Clause
SQL ‐ Select The SELECT clause is use to make queries against data held in one of more tables in your database.
The basic syntax of the select clause ‐
SELECT column-name1, column-name2, column-name3, column-nameN
from table-name;
SQL: SELECT Statement
A basic SELECT statement includes 3 clauses
SELECT <attribute name> FROM <tables> WHERE <condition>
SELECT
Specifies the attributes that are part of the resulting relation
FROM
Specifies the tables that serve as the input to the statement
WHERE
Specifies the selection condition, including the join condition.
Note: that you don't need to use WHERE
Using a “*” in a select statement indicates that every attribute of the input table is to be selected.Example: SELECT * FROM … WHERE …;
To get unique rows, type the keyword DISTINCT after SELECT.Example: SELECT DISTINCT * FROM …
WHERE …;
SQL: SELECT Statement (cont.)
7
SELECT Statement Clauses of the SELECT statement:
SELECT List the columns (and expressions) that should be returned from the query
FROM Indicate the table(s) or view(s) from which data will be obtained
WHERE Indicate the conditions under which a row will be included in the result
GROUP BY Indicate columns to group the results
HAVING Indicate the conditions under which a group will be included
ORDER BY Sorts the result according to specified columns
8
Figure 7-8: SQL statement processing order
SELECT Example Find products with standard price less than $275
SELECT PRODUCT_NAME, STANDARD_PRICE FROM PRODUCT_V WHERE STANDARD_PRICE < 275;
10
SELECT Example using Alias Alias is an alternative column or table name
SELECT CUST.CUSTOMER AS NAME, CUST.CUSTOMER_ADDRESS
FROM CUSTOMER_V CUSTWHERE NAME = ‘Home Furnishings’;
SELECT Example Using a Function
Using the COUNT aggregate function to find totals Aggregate functions: SUM(), MIN(), MAX(), AVG(), COUNT()
SELECT COUNT(*) FROM ORDER_LINE_VWHERE ORDER_ID = 1004;
13
SELECT Example – Boolean Operators AND, OR, and NOT Operators for customizing conditions in WHERE clause
SELECT PRODUCT_DESCRIPTION, PRODUCT_FINISH, STANDARD_PRICE
FROM PRODUCT_VWHERE (PRODUCT_DESCRIPTION LIKE ‘%Desk’OR PRODUCT_DESCRIPTION LIKE ‘%Table’) AND UNIT_PRICE > 300;
Note: the LIKE operator allows you to compare strings using wildcards. For example, the % wildcard in ‘%Desk’ indicates that all strings that have any number of characters preceding the word “Desk” will be allowed
SELECT Example –Sorting Results with the ORDER BY Clause
Sort the results first by STATE, and within a state by CUSTOMER_NAME
SELECT CUSTOMER_NAME, CITY, STATEFROM CUSTOMER_VWHERE STATE IN (‘FL’, ‘TX’, ‘CA’, ‘HI’)ORDER BY STATE, CUSTOMER_NAME;
Note: the IN operator in this example allows you to include rows whose STATE value is either FL, TX, CA, or HI. It is more efficient than separate OR conditions
SELECT Example –Categorizing Results Using the GROUP BY Clause
SELECT STATE, COUNT(STATE) FROM CUSTOMER_VGROUP BY STATE;
Note: you can use single‐value fields with aggregate functions if they are included in the GROUP BY clause
SELECT Example –Qualifying Results by Categories Using the HAVING Clause
For use with GROUP BY
SELECT STATE, COUNT(STATE) FROM CUSTOMER_VGROUP BY STATEHAVING COUNT(STATE) > 1;
Like a WHERE clause, but it operates on groups (categories), not on individual rows. Here, only those groups with total numbers greater than 1 will be included in final result
Using and Defining Views Views provide users controlled access to tables
Base Table – table containing the raw data
Dynamic View A “virtual table” created dynamically upon request by a user No data actually stored; instead data from base table made available to user
Based on SQL SELECT statement on base tables or other views
Sample CREATE VIEWCREATE VIEW EXPENSIVE_STUFF_V ASSELECT PRODUCT_ID, PRODUCT_NAME, UNIT_PRICEFROM PRODUCT_TWHERE UNIT_PRICE >300;
View has a nameView is based on a SELECT statement
19
Advantages of Views
Simplify query commands Assist with data security (but don't rely on views for security, there are more important security measures)
Enhance programming productivity Contain most current base table data Use little storage space Provide customized view for user
20
Disadvantages of Views Use processing time each time view is referenced May or may not be directly updateable
SQL: Join operationA join can be specified in the FROM clause which list the two input relations and the WHERE clause which lists the join condition.Example:
Biotech1003Sales1002IT1001DivisionID
TN1002MA1001CA1000StateID
Emp Dept
SQL: Join operation (cont.)
Sales1002IT1001Dept.DivisionDept.ID
TN1002MA1001Emp.StateEmp.ID
inner join = joinSELECT *
FROM emp join dept (or FROM emp, dept) on emp.id = dept.id;
SQL: Join operation (cont.)
IT1001Sales1002
nullnullDept.DivisionDept.ID
CA1000
TN1002MA1001
Emp.StateEmp.ID
left outer join = left joinSELECT *
FROM emp left join depton emp.id = dept.id;
SQL: Join operation (cont.)
Sales1002Biotech1003
IT1001Dept.DivisionDept.ID
MA1001
nullnullTN1002
Emp.StateEmp.ID
right outer join = right joinSELECT *
FROM emp right join depton emp.id = dept.id;
SQL Union1. You can combine the output from two or more
queries/views by using the union clause.2. The general form is ‐Select col1, col2 from table1 where col1 = ‘xxx’UnionSelect col3, col4 from table2 where col3 = ‘yyy’
Rules, the selected columns from each query must match in type, the output column must be the same type.
SQL Correlated sub queryConsider this query ‐ we want to list all customers who have place no orders.
We could try and do this with left outer joins.But we can to a correlated subquery.
Select CustomerID From Customer CWhere not exists (select ‘a’ from CustomerOrder CO where CO.Customerid = C.Customerid)
The sub query returns a true/false condition.Select ‘a’ is faster then returning a column.
Learning outcomes – LO21. Be able to implement user Interfaces.
1. requirements e.g. 1. functionality, 2. reliability, 3. consistency, 4. performance, 5. menu driven, 6. HCI interface
Most of the SQL in DML and DQL will be needed to address the functional requirements of the user interface (1 thru 4).
Learning outcomes – LO2LO22.3 apply a range of database tools and techniques to enhance the user interface.This is putting our SQL knowledge to use in an interface.3.3 critically evaluate how meaningful data has been extracted through the use of query tools.Here we need to examine the use of SQL has been used to deliver business data to the user.
Learning outcomes – LO2A couple of terms we need to know -
Verification –Verification means to check that the data on the original source document is identical to the data that you have entered into the system or object document.
Validation –Validation aims to make sure that data is sensible, reasonable, complete and within acceptable boundaries.
Validation is a user constraint not a database constraint!
HCIUser interface: requirements eg functionality, reliability, consistency, performance, menu driven, HCI interface
We need to create a simple interface! Sorry.
For this we can use Excel forms or Microsoft Access or php if you are able or something else.
I will demonstrate how to connect a Sql Server and MySql database to Microsoft Access 2013.
Learning outcomes – LO2Now you can do Lab07.
Assignment 2 is now issued.
Summary
What have we learnt today?