Sql server ___________ (advance sql)

Post on 14-Apr-2017

246 views 12 download

transcript

Advanced Query Techniques

Session Objectives Explain the UNION, EXCEPT and

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

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 ..

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

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:

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:

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

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:

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.

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.

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.

Example of using INTERSECT

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.

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.

Example of using EXCEPT

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:

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’.

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.

Example of using IN

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:

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.

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’

)

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.

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

Output of Example 2