We are creating more SQL queries!

Post on 07-Feb-2016

25 views 0 download

description

We are creating more SQL queries!. Login to SQL Server 2012 Management Studio If you weren’t here last class, execute the script file called “create-emp1.sql ”. It is located on the k: drive in the IS475\f12 directory. What is the goal of a SQL query?. - PowerPoint PPT Presentation

transcript

We are creating more SQL queries!

Login to SQL Server 2012 Management Studio

If you weren’t here last class, execute the script file called “create-emp1.sql”.

It is located on the k: drive in the IS475\f12 directory.

What is the goal of a SQL query?

• To produce an accurate result table.• To produce an accurate result table that contains

meaningful information.• To produce an accurate result table that contains

meaningful information that will help solve a business problem.

• To produce an accurate result table that contains meaningful information that will help solve a business problem and is capable of being viewed through a front-end visualization program to make an impact.

What is the syntax of a SQL query?

SELECT (list of columns)

FROM (tables with declaration of inner and/or outer joins with join conditions)

WHERE (condition for each row)

GROUP BY (summary control break field(s))

HAVING (condition for each group)

ORDER BY (sort columns)

Revisit last class • Single row queries.

– Designed to process individually each row of the underlying table and produce a result table.

– Usually includes fewer columns and rows than underlying table(s).

– May include calculations and functions.– Rows selected with the WHERE clause.– Columns selected with the SELECT clause.

Sample single row query

SELECT ename "Employee Name",hiredate "Date Hired",MONTH(hiredate) "Month Integer",DATEDIFF(MONTH, hiredate, getdate())

"Number of Months Employed",salary,commission,salary + ISNULL(commission,0)

"Total Compensation"FROM emp1WHERE deptno = 30 and salary <=3000;

Aggregate (Group) Queries• Summarizes data and provides more meaningful and

informative output from the database. Sometimes referred to as “summary queries.”

• Aggregate/group functions differ from single row SELECT statements:– A SELECT statement processes every row in the underlying table. The result

table (unless a WHERE clause is used) contains one row per row in the underlying table.

– An aggregate function collects data from multiple rows and produces summarized data in the result table. There should be one row in the result table per aggregate group.

• If an aggregate function is run on the whole table, without grouping, it generates a single row result table.

• If an aggregate function is run with grouping , then it generates one row per group in the result table.

Function Description of What is ReturnedAVG Average value of a numeric column; ignores

null values

COUNT Number of rows. When * is used, all rows are returned (including null values and duplicate rows)

MAX Maximum value of a column; ignores null values

MIN Minimum value of a column; ignores null values

SUM Totals the value of a numeric column; ignores null values

7

What are the aggregate functions in SQL?

Sample Aggregate QueriesSELECT COUNT(empno),

SUM(salary), MIN(salary)

FROM emp1;

SELECT deptno, SUM(salary)

FROM emp1GROUP BY deptno;

SELECT deptno, SUM(salary)

FROM emp1GROUP BY deptnoHAVING SUM(salary) > 15000;

Getting data from multiple tables• Why do you want to access data from multiple

tables in a single query?– To provide more complete information in a result table.

– To support decision making.

• SQL programmers need to understand what happens when multiple tables are accessed in a single query.

Time for some new tables!!

Create the two tables above with the script file called “JoinLab1.sql”.

It is located on the k: drive in the IS475\f12 directory.

Questions about design on previous page

• Does the design indicate whether or not referential integrity is enforced in the database?

• Does the inclusion of a foreign key to relate tables imply that referential integrity is enforced in the database?

• What does it mean to say “referential integrity is enforced” vs. “referential integrity is not enforced” in a database?

• Is it necessary to enforce referential integrity to relate tables in a relational database?

Order ID OrderDate CustID DueDate100 9/23/2012 1234 10/1/2012

200 9/24/2012 6773 11/1/2012

300 9/22/2012 1234 10/15/2012

CustID CustomerName1234 John Smith2555 Jane Doe6773 Bertie Wooster8372 Martin Cheng

tblOrder tblCustomer

Order ID OrderDate DueDate CustomerName

100 9/23/2012 10/1/2012 John Smith200 9/24/2012 11/1/2012 Bertie Wooster300 9/22/2012 10/15/2012 John Smith

+Result Table

=

SELECT *FROM tblOrder,

tblCustomer

Order ID OrderDate CustID DueDate100 9/23/2012 1234 10/1/2012

200 9/24/2012 6773 11/1/2012

300 9/22/2012 1234 10/15/2012

CustID CustomerName1234 John Smith2555 Jane Doe6773 Bertie Wooster8372 Martin Cheng

OrderID OrderDate CustID DueDate CustID CustomerName100 9/23/2012 1234 10/1/2012 1234 John Smith

100 9/23/2012 1234 10/1/2012 2555 Jane Doe

100 9/23/2012 1234 10/1/2012 6773 Bertie Wooster

100 9/23/2012 1234 10/1/2012 8372 Martin Cheng200 9/24/2012 6773 11/1/2012 1234 John Smith

200 9/24/2012 6773 11/1/2012 2555 Jane Doe

200 9/24/2012 6773 11/1/2012 6773 Bertie Wooster200 9/24/2012 6773 11/1/2012 8372 Martin Cheng300 9/22/2012 1234 10/15/2012 1234 John Smith300 9/22/2012 1234 10/15/2012 2555 Jane Doe300 9/22/2012 1234 10/15/2012 6773 Bertie Wooster300 9/22/2012 1234 10/15/2012 8372 Martin Cheng

+

Cartesian Product

Cross Join

Or

OrderID OrderDate CustID DueDate CustID CustomerName100 9/23/2012 1234 10/1/2012 1234 John Smith

100 9/23/2012 1234 10/1/2012 2555 Jane Doe

100 9/23/2012 1234 10/1/2012 6773 Bertie Wooster

100 9/23/2012 1234 10/1/2012 8372 Martin Cheng200 9/24/2012 6773 11/1/2012 1234 John Smith

200 9/24/2012 6773 11/1/2012 2555 Jane Doe

200 9/24/2012 6773 11/1/2012 6773 Bertie Wooster200 9/24/2012 6773 11/1/2012 8372 Martin Cheng300 9/22/2012 1234 10/15/2012 1234 John Smith300 9/22/2012 1234 10/15/2012 2555 Jane Doe300 9/22/2012 1234 10/15/2012 6773 Bertie Wooster300 9/22/2012 1234 10/15/2012 8372 Martin Cheng

100 9/23/2012 1234 10/1/2012 1234 John Smith

200 9/24/2012 6773 11/1/2012 6773 Bertie Wooster

300 9/22/2012 1234 10/15/2012 1234 John Smith

SELECT *FROM tblOrderINNER JOIN tblCustomerON tblOrder.custID = tblCustomer.custIDORDER BY tblOrder.orderID

OrderID OrderDate CustID DueDate CustID CustomerName100 9/23/2012 1234 10/1/2012 1234 John Smith

100 9/23/2012 1234 10/1/2012 2555 Jane Doe

100 9/23/2012 1234 10/1/2012 6773 Bertie Wooster

100 9/23/2012 1234 10/1/2012 8372 Martin Cheng200 9/24/2012 6773 11/1/2012 1234 John Smith

200 9/24/2012 6773 11/1/2012 2555 Jane Doe

200 9/24/2012 6773 11/1/2012 6773 Bertie Wooster200 9/24/2012 6773 11/1/2012 8372 Martin Cheng300 9/22/2012 1234 10/15/2012 1234 John Smith300 9/22/2012 1234 10/15/2012 2555 Jane Doe300 9/22/2012 1234 10/15/2012 6773 Bertie Wooster300 9/22/2012 1234 10/15/2012 8372 Martin Cheng

200 9/24/2012 6773 11/1/2012 6773 Bertie Wooster

100 9/23/2012 1234 10/1/2012 1234 John Smith

300 9/22/2012 1234 10/15/2012 1234 John Smith

SELECT *FROM tblCustomerINNER JOIN tblOrderON tblOrder.custID = tblCustomer.custIDORDER BY tblOrder.orderID

Open a new query window, and type the SQL code below. This code has the customer table placed first in the FROM statement. How do the results contrast with the SQL code on slide #18?

SELECT ord.orderid,ord.orderdate,ord.duedate,cust.customername

FROM tblOrder ordINNER JOIN tblCustomer custON Ord.custID = Cust.custIDORDER BY ord.orderid

Order ID OrderDate DueDate CustomerName

100 9/23/2012 10/1/2012 John Smith200 9/24/2012 11/1/2012 Bertie Wooster300 9/22/2012 10/15/2012 John Smith

Finalize the query by SELECTing only the required columns

Let’s make a new query!

Order ID OrderDate CustID DueDate100 9/23/2012 1234 10/1/2012

200 9/24/2012 6773 11/1/2012

300 9/22/2012 1234 10/15/2012

CustID CustomerName1234 John Smith2555 Jane Doe6773 Bertie Wooster8372 Martin Cheng

tblOrder tblCustomer

+CustomerName OrderID DueDate

Bertie Wooster 200 11/1/2012

Jane Doe No order

John Smith 100 10/1/2012

John Smith 300 10/15/2012

Martin Cheng No order

=

SELECT cust.CustomerName,ISNULL(ord.orderID, ‘No Order’) OrderID,ord.DueDate

FROM tblOrder ordINNER JOIN tblCustomer custON Ord.custID = Cust.custIDORDER BY cust.customername

CustomerName OrderID DueDateBertie Wooster 200 11/1/2012

John Smith 100 10/1/2012

John Smith 300 10/15/2012

SELECT cust.CustomerName,ISNULL(ord.orderID, ‘No Order’) OrderID,ord.DueDate

FROM tblOrder ordRIGHT OUTER JOIN tblCustomer custON Ord.custID = Cust.custIDORDER BY cust.customername

CustomerName OrderID DueDateBertie Wooster 200 11/1/2012

Jane Doe No order

John Smith 100 10/1/2012

John Smith 300 10/15/2012

Martin Cheng No order

FROM tblOrder RIGHT OUTER JOIN tblCustomer

tblOrder tblCustomer+ = Result Table

Left Side of the join

Right Side of the join

Let’s say that referential integrity is not enforced and we

have more rows in our tables...

Order ID OrderDate CustID DueDate100 9/23/2012 1234 10/1/2012

200 9/24/2012 6773 11/1/2012

300 9/22/2012 1234 10/15/2012

400 9/27/2012 2555 10/16/2012

500 9/12/2012 8989 9/22/2012

600 9/23/2012 2555 9/27/2012

700 9/15/2012 2555 11/1/2012

CustID CustomerName1234 John Smith2555 Jane Doe6773 Bertie Wooster8372 Martin Cheng

Execute the script file called: JoinLabExpand1.sql on the k: drive in the IS475\f12 directory to create a table called “tblOrder1”.

How many rows and columns in the

cartesian product (cross join)?

SELECT *FROM tblOrder1,

tblCustomer

What would the results look like from an inner join?

SELECT *FROM tblOrder1INNER JOIN tblCustomerON tblOrder1.custID = tblCustomer.custID

Order ID OrderDate CustID DueDate C.CustID CustomerName

100 9/23/2012 1234 10/1/2012 1234 John Smith200 9/24/2012 6773 11/1/2012 6773 Bertie Wooster300 9/22/2012 1234 10/15/2012 1234 John Smith

400 9/27/2012 2555 10/16/2012 2555 Jane Doe

600 9/23/2012 2555 9/27/2012 2555 Jane Doe

700 9/15/2012 2555 11/1/2012 2555 Jane Doe

What is missing??

What would the results look like from a right outer join?

SELECT *FROM tblOrder1RIGHT OUTER JOIN tblCustomerON tblOrder1.custID = tblCustomer.custID

Order ID OrderDate CustID DueDate C.CustID CustomerName100 9/23/2012 1234 10/1/2012 1234 John Smith200 9/24/2012 6773 11/1/2012 6773 Bertie Wooster300 9/22/2012 1234 10/15/2012 1234 John Smith400 9/27/2012 2555 10/16/2012 2555 Jane Doe600 9/23/2012 2555 9/27/2012 2555 Jane Doe700 9/15/2012 2555 11/1/2012 2555 Jane Doe

8372 Martin Cheng

The row is still missing...

What would the results look like from a left outer join?

SELECT *FROM tblOrder1LEFT OUTER JOIN tblCustomerON tblOrder1.custID = tblCustomer.custID

Order ID OrderDate CustID DueDate C.CustID CustomerName

100 9/23/2012 1234 10/1/2012 1234 John Smith200 9/24/2012 6773 11/1/2012 6773 Bertie Wooster300 9/22/2012 1234 10/15/2012 1234 John Smith

400 9/27/2012 2555 10/16/2012 2555 Jane Doe

500 9/12/2012 8989 9/22/2012

600 9/23/2012 2555 9/27/2012 2555 Jane Doe

700 9/15/2012 2555 11/1/2012 2555 Jane Doe

All rows from both tables!

SELECT *FROM tblOrder1FULL OUTER JOIN tblCustomerON tblOrder1.custID = tblCustomer.custID

Order ID OrderDate CustID DueDate C.CustID CustomerName100 9/23/2012 1234 10/1/2012 1234 John Smith200 9/24/2012 6773 11/1/2012 6773 Bertie Wooster300 9/22/2012 1234 10/15/2012 1234 John Smith400 9/27/2012 2555 10/16/2012 2555 Jane Doe500 9/12/2012 8989 9/22/2012600 9/23/2012 2555 9/27/2012 2555 Jane Doe700 9/15/2012 2555 11/1/2012 2555 Jane Doe

8372 Martin Cheng

Write a queryWrite a query that displays all the orders placed by the customer “Jane Doe”. Assume that you don’t know Jane Doe’s customer ID and have to use the customer name in the WHERE clause.

The result table should look like the one provided below.

Order ID OrderDate DueDate

400 9/27/2012 10/16/2012

600 9/23/2012 9/27/2012

700 9/15/2012 11/1/2012

Does the query change if the database DOES enforce referential integrity?

Write another queryWrite a query that displays all the orders that don’t have a valid customer.

The result table should look like the one provided below.

Order ID OrderDate CustID DueDate C.CustID CustomerName500 9/12/2012 8989 9/22/2012

Last 2 queries of the day…First, write a query that summarizes order data by customer. The result table should look like the one provided below.

CustID CustomerName CountofOrders

1234 John Smith 2

2555 Jane Doe 3

6773 Bertie Wooster 1

Second, change it so that all customers are displayed, whether or not they have an order. The result table should look like the one provided below.

CustID CustomerName CountofOrders

1234 John Smith 2

2555 Jane Doe 3

6773 Bertie Wooster 1

8372 Martin Cheng 0