Sie sind auf Seite 1von 20

Subqueries and Joins

SUB QUERIES
A sub query can be defined as a group of nested SELECT statement inside a SELECT, INSERT, UPDATE, or DELETE statement.

Syntax (SELECT [ALL | DISTINCT] subquery_select_list [FROM {table_name2 | view_name2}] [, {view_name}]] [WHERE clause] [GROUP BY clause] [HAVING clause])

Subqueries With EXISTS

A subquery, when used with the EXISTS clause, always returns data in terms of a TRUE or FALSE value.
It checks for the existence of data row according to the condition specified in the inner query and passes the existence status to the outer query to produce the result set. The subquery returns a TRUE value if a subquery contains any row.

SELECT Publisher = Pub_Name FROM Publishers WHERE EXISTS (SELECT Pub_Id FROM Titles WHERE Type = business)

SELECT Publisher = Pub_Name FROM Publishers WHERE EXISTS (SELECT * FROM Publishers WHERE City = Paris)

Correlated Subqueries

A correlated subquery can be defined as a query that depends on the outer query for its evaluation. In a correlated subquery, the WHERE clause references a table in the FROM clause of the outer query. In the case of correlated subqueries, the inner query is evaluated for each row of the table specified in the outer query.

Queries with Modified Comparison Operators

SQL Server provides the ALL and ANY keywords that can be used to modify the existing comparison operator. The subquery introduced with the modified comparison operator returns zero or more values and can be implemented using the GROUP BY or HAVING clauses.

JOINS

Join conditions can be specified in either the FROM or WHERE clauses; specifying them in the FROM clause is recommended. WHERE and HAVING clauses can also contain search conditions to further filter the rows selected by the join conditions. Joins can be categorized as: 1. Inner joins (the typical join operation, which uses some comparison operator like = or <>). These include equi-joins and natural joins. Inner joins use a comparison operator to match rows from two tables based on the values in common columns from each table. For example, retrieving all rows where the student identification number is the same in both the students and courses tables.

2. Outer joins. Outer joins can be a left, right, or full outer join. Outer joins are specified with one of the following sets of keywords when they are specified in the FROM clause: a. LEFT OUTER JOIN The result set of a left outer join includes all the rows from the left table specified in the LEFT OUTER clause, not just the ones in which the joined columns match. When a row in the left table has no matching rows in the right table, the associated result set row contains null values for all select list columns coming from the right table. b. RIGHT OUTER JOIN A right outer join is the reverse of a left outer join. All rows from the right table are returned. Null values are returned for the left table any time a right table row has no matching row in the left table. c. FULL OUTER JOIN A full outer returns all rows in both the left and right tables. Any time a row has no match in the outer table, the select list columns from the other table contain null values. When there is a match between the tables, the entire result set contain data values from the base tables.

Example : SELECT pub_name, au_lname, au_fname FROM Publishers p LEFT OUTER JOIN Authors a ON p.City=a.City

SELECT s.Stor_Id, s.Ord_Date, t.Title_Id FROM Sales s RIGHT OUTER JOIN Titles t ON s.Title_Id = t.Title_Id AND DATENAME (yy, s.Ord_Date) = '1999'

Das könnte Ihnen auch gefallen