Top Banner
T-SQL – 3 rd session Medhat Dawoud http://www.medhatdawoud.com [email protected] Twitter @Med7atDawoud
31

Intro to t sql – 3rd session

May 19, 2015

Download

Education

Medhat Dawoud

24 may 2011 in DevMix free course
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Intro to t sql – 3rd session

T-SQL – 3rd session

Medhat Dawoudhttp://[email protected]

Twitter @Med7atDawoud

Page 2: Intro to t sql – 3rd session

Agenda1. Nulls2. Order by

3. Distinct

4. Aggregates

5. Grouping

6. Having vs. where

7. Compute by

8. Union

9. Constraints (PK,FK)

10. Join tables

11. Sub queries, nested queries

12. Common restriction on Sub-queries

Page 3: Intro to t sql – 3rd session

Nulls

• A null does not mean zero

• A null indicates that a value is missing, unavailable, incomplete, and inapplicable.

• Nulls represent an unknown quantity or value.

• Any question about a null could provide three answers: yes, no, or maybe

Try it Now

Page 4: Intro to t sql – 3rd session

Remember that

• studying null gives you the full ability to select any data from a database.

• The select clause specifies what columns we want to see

• The from clause tells what table we want to see data from

• The where clause restricts the data we will see

Page 5: Intro to t sql – 3rd session

Order by

• The order by clause is used to specify a sorting order of the result set

• The sorting can be performed by column name or by column number.

• The default sort order is ascending (a-z), but you can specify a descending order by using the keyword desc.

Try it Now

Page 6: Intro to t sql – 3rd session

Distinct• Try this: select studentName from libraryVisitors

• As you have seen from the above query result, you can get what appear to be duplicate rows in the result set

• From the scope of the result set, they are duplicates

• From the scope of the database they are not

• Sometimes we do not want to see these duplicate rows

• We can eliminate them by use of the distinct keywordTry it Now

Page 7: Intro to t sql – 3rd session

Aggregates

• The three we will explore are count, sum, and average.

• count returns a count of the number of rows in a table that match a certain criteria

• sum is used to add up all of the values in a column

• Avg will return the average value in a column

Try it Now

Page 8: Intro to t sql – 3rd session

Group by

• Data in a table is essentially stored randomly

• The group by will order the data into groups

• You still need to specify an order by clause to perform sorting

• Nulls are consider a group

• The true power of a group by comes from using it in conjunction with an aggregate

Try it Now

Page 9: Intro to t sql – 3rd session

Group by

• Note that: One thing to remember is that if you use a group by with an aggregate, you must specify all non-aggregate columns in the group by clause.

• You can not specify an aggregate in the group by clause.

Try it Now

Page 10: Intro to t sql – 3rd session

Having vs. where• There is a fundamental difference

• The where clause defines the set of data the grouping is done on

• The having defines which groups are going to be returned to the user

• Having clause generally contain aggregates as part of the selection criteria

• The book "The Practical SQL Handbook" has a good explanation on pages 180 - 185

Try it Now

Page 11: Intro to t sql – 3rd session

Compute by

• With a compute/computed by, you can only use columns in the select list

• A compute by is used to sub-summaries

• You can use any aggregate except count(*)

• Compute by must start with the same expressions as listed after order by and not skip any expressions

Try it Now

Page 12: Intro to t sql – 3rd session

Compute by

Legal• order by a,b,c• compute by a,b,c• compute by a,b• compute avg(price) by a

Illegal• order by a,b,c• compute by b,a,c• compute by c,a• compute avg(price) by b

Try it Now

Page 13: Intro to t sql – 3rd session

Union

• combining data from two different tables when they have mutually exclusive criteria

• The only restrictions on unions are that the same number of columns must be in each separate result set and the datatypes must match

• We use the keyword Union to do that

Try it Now

Page 14: Intro to t sql – 3rd session

Relationships• A database derives its usefulness from containing a group of

tables that have some relationship to each other

• An entity is a person, place, or thing of importance to an organization

• An entity generally becomes a table

• Relationships are the connections between tables

• Relationships are usually implemented as keys in a database design

Page 15: Intro to t sql – 3rd session

Relationships cont.

Relationships come in three different varieties

One to one• One row in a table is related to exactly one row in another table

One to many• One row in a table is related to one or more rows in another table

Many to many• Many rows in a table are related to one or more rows in another

table

Page 16: Intro to t sql – 3rd session

Primary Key

• A primary key is a special type of key that consists of one or more columns that uniquely identify a row

• Primary keys must be unique and can not contain null values

• A table will only have one primary key

• Primary key can not be null.

Try it Now

Page 17: Intro to t sql – 3rd session

Foreign Key

• A foreign key is one or more columns that refer to a primary key of another table

• If we have stu_id as a primary key in students table it’s consider to be a foreign key in the libraryVisitors table.

Try it Now

Page 18: Intro to t sql – 3rd session

Composite key

• A primary key and a foreign key can consist of more than one column

• When a key contains more than one column, it is known as a composite key

• The primary key of the titleauthor table is a composite (au_id,title_id)

Page 19: Intro to t sql – 3rd session

Join tables

• All of the data in a database is segmented into tables and we generally need data from more than one table to show what we need

• You will notice that there is no such thing as a join clause in our SQL syntax

• If we get the data from library table we will get the stu_id which make no sense we need the studentName so, we use joins as shown in the try it out.

Try it Now

Page 20: Intro to t sql – 3rd session

Join tables (PUBS)

title_id = title_id

title_id = title_id

title_id = title_id

stor_id = stor_id

stor_id = stor_id

pub_id = pub_id

pub_id = pub_id

pub_id = pub_id

job_id = job_id

au_id = au_id

authors

au_id varchar(11)au_lname varchar(40)au_fname varchar(20)phone char(12)address varchar(40)city varchar(20)state char(2)zip char(5)contract bit

authors_tmp

au_lname varchar(40)au_fname varchar(20)phone char(12)address varchar(40)city varchar(20)state char(2)zip char(5)

discounts

discounttype varchar(40)stor_id char(4)lowqty smallinthighqty smallintdiscount decimal

employee

emp_id char(9)fname varchar(20)minit charlname varchar(30)job_id smallintjob_lvl tinyintpub_id char(4)hire_date datetime

jobs

job_id smallintjob_desc varchar(50)min_lvl tinyintmax_lvl tinyint

pub_info

pub_id char(4)logo imagepr_info text

publishers

pub_id char(4)pub_name varchar(40)city varchar(20)state char(2)country varchar(30)

roysched

title_id varchar(6)lorange inthirange introyalty int

sales

stor_id char(4)ord_num varchar(20)ord_date datetimeqty smallintpayterms varchar(12)title_id varchar(6)

stores

stor_id char(4)stor_name varchar(40)stor_address varchar(40)city varchar(20)state char(2)zip char(5)

titleauthor

au_id varchar(11)title_id varchar(6)au_ord tinyintroyaltyper int

titles

title_id varchar(6)title varchar(80)type char(12)pub_id char(4)price moneyadvance moneyroyalty intytd_sales intnotes varchar(200)pubdate datetime

Page 21: Intro to t sql – 3rd session

Join tables

• We are retrieving data from more than one table, so each table must be specified in the from clause

• So, The from clause can be seen as the main driver of a SQL statement

• The type of join we have examined so far is also referred to as an equi-join or an inner join

• And if you will notice that the results that has null values is ignored

Page 22: Intro to t sql – 3rd session

Join tables• To solve the problem of the null values we use another type of joins called outer

join

• Outer joins come in three different flavors– Left– Right– Full

• Left: stores.stor_id *= sales.stor_id• Right: sales.stor_id =* stores.stor_id

• A full outer join is included here for completeness

• If you have one table with 100 rows and another with 1000 rows, a full outer join will produce a result set of 100,000 rows

Try it Now

Page 23: Intro to t sql – 3rd session

Full outer Join

• A full outer join will produce a cross product of the two tables

• This is because with a full outer join, you are telling the database to give every combination of rows possible

• The first time you inadvertently fire one of these off, you will get a rather angry call from your DBA

Page 24: Intro to t sql – 3rd session

Sub queries

• Sub-queries are simply a SQL statement nested inside of another SQL statement.

• The most common place to do this is in a where or having clause.

• Subqueries come in two basic kinds: correlated and noncorrelated

Page 25: Intro to t sql – 3rd session

Two kinds of sub queries

• A noncorrelated sub-query is one in which the inner query is independent, gets evaluated first, and passes it’s result set back to the outer query

• A correlated sub-query is one in which the inner query is dependent upon the results from the outer query

Page 26: Intro to t sql – 3rd session

Two examples to sub queries

• noncorrelated:select pub_name from publisherswhere pub_id in (select pub_id from titleswhere type = 'business')

• correlated:select pub_name from publishers pwhere 'business' in (select type from titles where oub_id = p.pub_id)

Try it Now

Page 27: Intro to t sql – 3rd session

Join and sub-queriesselect distinct pub_name from publishers, authors where publishers.city = authors.city

ANDselect pub_name from publishers where city in (select city from authors)

will return the same results

• Whether you use joins or subqueries is usually a matter of choice

• Most joins can be expressed as subqueries and vice versa

Page 28: Intro to t sql – 3rd session

Common restriction

• Subqueries can not manipulate their results internally. i.e. They can not contain an order by or the keyword INTO

• You use the ANY and ALL keywords with a comparison operator in a sub-query

• > ALL means greater than every value in the results of the inner query (> maximum value)

• > ANY means greater than any value in the results of the inner query (> minimum value)

Try it Now

Page 29: Intro to t sql – 3rd session

Exist

• The last type of sub-query is used to test for the existence of something

• To find all of the publishers who publish business books we would do the following:

select distinct pub_name from publishers where exists (select 1 from titles where pub_id = publishers.pub_id and type = 'business')

Page 30: Intro to t sql – 3rd session

Nested Queries

• A sub-query may contain another sub-query

• In fact you can nest as many levels as you need. However, for most applications more than four levels is an indication of poor database design

Page 31: Intro to t sql – 3rd session

The End