+ All Categories
Home > Education > Sql server ___________ (advance sql)

Sql server ___________ (advance sql)

Date post: 14-Apr-2017
Category:
Upload: ehtisham-ali
View: 246 times
Download: 12 times
Share this document with a friend
25
Advanced Query Techniques
Transcript
Page 1: Sql server  ___________  (advance sql)

Advanced Query Techniques

Page 2: Sql server  ___________  (advance sql)

Session Objectives Explain the UNION, EXCEPT and

INTERSECT operators. Explain subqueries. Describe joins. Describe the various types of joins.

Page 3: Sql server  ___________  (advance sql)

Introduction In large enterprises, there are

huge volumes of data stored in databases.

Data can be spread over several tables that are related to one another.

When data is stored in such tables, there must be some means to combine and retrieve the data from those tables.

Using SQL Server, there are a number of ways to combine data from multiple tables.

Database

Table 1Table 2

Table N

Table ..

Page 4: Sql server  ___________  (advance sql)

Combining Data Using Operators

SQL Server provides certain keywords, also called as operators, to combine data from multiple tables. These operators are: UNION INTERSECT EXCEPT

Page 5: Sql server  ___________  (advance sql)

UNION Operator - 1

Combines results from two different query statements into a single resultset.

Syntax: Query_Statement1 UNION [ALL]Query_Statement2

where,

Query_Statement1 and Query_Statement2 are SELECT statements.

UNION Operator:

Page 6: Sql server  ___________  (advance sql)

Example of using UNION -1

Journals:

Books:

Consider that you want to see the title_id and title of all the books and journals in an online library. To accomplish this, the records from both the tables have to be retrieved. The data in the tables are as follows:

Page 7: Sql server  ___________  (advance sql)

Records of Books table are displayed first, followed by those in Journals table. This is because the records are sorted by default on the basis of the first column. In this case, they are sorted on title_id.

Example of using UNION -2

Page 8: Sql server  ___________  (advance sql)

ALL clause of UNION OperatorBy default, the UNION operator removes duplicate records from the resultset. But, if you use the ALL clause with UNION operator, then all the rows are returned.

Consider that you have an additional table named Publications with the following data in the online library, and that the table has records as shown:

Page 9: Sql server  ___________  (advance sql)

Example of ALL with UNIONThe figure shows how to combine the data in the tables Books and Publications using UNION with ALL. The query displays all the records from both the tables.

Page 10: Sql server  ___________  (advance sql)

INTERSECT Operator -1The tables Publications and Books contain four records each:

Consider that you want to display only the rows that are common to both the tables. To do this, you will need to use an operator named INTERSECT.

Page 11: Sql server  ___________  (advance sql)

INTERSECT Operator -2The INTERSECT operator is used with two query statements to return a distinct set of rows that are common to both the query statements.

Syntax: Query_Statement1 INTERSECTQuery_Statement2

where, Query_Statement1 and Query_Statement2 are SELECT statements.

Page 12: Sql server  ___________  (advance sql)

Example of using INTERSECT

Page 13: Sql server  ___________  (advance sql)

Rules for using INTERSECT

The basic rules for using INTERSECT are:

The number of columns and the order in which they are given must be the same in both the queries.

The data types of the columns being used must be compatible.

Page 14: Sql server  ___________  (advance sql)

EXCEPT Operator

The two rules that apply to INTERSECT operator are also applicable for EXCEPT operator.

Syntax:

Query_Statement1 EXCEPTQuery_Statement2

The EXCEPT operator returns all of the distinct rows from the query given on the left of the EXCEPT operator and removes all the rows from the resultset that match the rows on the right of the EXCEPT operator.

Page 15: Sql server  ___________  (advance sql)

Example of using EXCEPT

Page 16: Sql server  ___________  (advance sql)

Subqueries You can use a SELECT statement or a query to return

records that will be used as criteria for another SELECT statement or query. The outer query is called parent query and the inner query is called a subquery.

The simplest form of a subquery is one which returns just one column. The parent query can use the results of this subquery using an = sign.

SELECT <ColumnName> FROM <table> WHERE <ColumnName> = ( SELECT <ColumnName> FROM <Table> WHERE <ColumnName> = <Condition> )

Syntax:

Page 17: Sql server  ___________  (advance sql)

Example of SubqueriesConsider that you want to know the names of titles from the database pubs which are published by the publisher ‘New Moon Books’.

Page 18: Sql server  ___________  (advance sql)

ANY, ALL, IN, and EXISTS You can use a subquery instead of a value in

a WHERE clause of a SELECT statement. ANY, ALL, IN, and EXISTS are keywords that

can be used with the WHERE clause of a SELECT statement when the query returns one column but one or more rows.

Page 19: Sql server  ___________  (advance sql)

Example of using IN

Page 20: Sql server  ___________  (advance sql)

EXISTS and NOT EXISTS The keyword EXISTS is used with a subquery to check the

existence of rows returned by the subquery. The subquery does not actually return any data; it returns a value of TRUE or FALSE.

SELECT <ColumnName> FROM <table>

WHERE [NOT] EXISTS

(

<Subquery_Statement>

)

Syntax:

Page 21: Sql server  ___________  (advance sql)

Example of EXISTSConsider that you want to determine if there are any publishers who publish books in the category of traditional cooking, that is, ‘trad_cook’. Here, you can use the EXISTS keyword to check the existence of any such books and then retrieve information about their publishers. Example 1 shows the code for this.

Page 22: Sql server  ___________  (advance sql)

Example 1USE pubs

SELECT pub_id, pub_name, city

FROM publishers

WHERE EXISTS

(

SELECT pub_id FROM titles

WHERE pub_id = publishers.pub_id

AND type = ‘trad_cook’

)

Page 23: Sql server  ___________  (advance sql)

Example of using NOT EXISTS Consider that you want to retrieve book details such as name, type and published date for only those books that

are published by publishers other than ‘New Moon Books’.

The condition could be summarized as “publishers not with pub_name New Moon Books”.

This would result in the publisher information who are other than ‘New Moon Books’.

To perform this task, you need to use the NOT EXISTS operator as shown in Example 2.

Page 24: Sql server  ___________  (advance sql)

Example 2USE pubsGOSELECT title, type, pubdateFROM titlesWHERE NOT EXISTS( SELECT * FROM publishers WHERE pub_id = titles.pub_id AND pub_name = ’New Moon Books’)

Page 25: Sql server  ___________  (advance sql)

Output of Example 2


Recommended