Presentation is loading. Please wait.

Presentation is loading. Please wait.

Select Complex Queries 98-364 Database Management Fundamentals LESSON 3.1b.

Similar presentations


Presentation on theme: "Select Complex Queries 98-364 Database Management Fundamentals LESSON 3.1b."— Presentation transcript:

1 Select Complex Queries 98-364 Database Management Fundamentals LESSON 3.1b

2 98-364 Database Management Fundamentals LESSON 3.1b Lesson Overview 3.1b Select data. This objective may include but is not limited to: queries to extract data from one table; extracting data by using joins; combining result sets by using UNION and INTERSECT. In this lesson, you will review:  Subqueries  UNIONS  JOINS  INTERSECTS

3 98-364 Database Management Fundamentals LESSON 3.1b Subqueries In Structured Query Language (SQL), a query can nest inside another query There are three basic types of subqueries. Type 1 Predicate—extended logical constructs in the WHERE (and HAVING ) clause(s) using the operators AND, OR, LIKE, BETWEEN, AS, and TOP(LIMIT). Example: SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern SELECT subject FROM class_info WHERE teacher LIKE “Sm”

4 98-364 Database Management Fundamentals LESSON 3.1b Subqueries (Continued) Type 2 Scalar—stand-alone queries that return a single value. Scalar subqueries can be used in CASE expressions, WHERE clauses, ORDER BY clauses, and SELECT clauses. Example: SELECT column_name(s) FROM table_name WHERE variable_1 = (SELECT column_name FROM table_name WHERE column_name = variable_2)

5 98-364 Database Management Fundamentals LESSON 3.1b Subqueries (Continued) Type 3 Table—queries nested in the FROM clause. SELECT table_name_1 FROM table_name_1, (SELECT column_name_2 FROM table_name_2 WHERE column_name_3 = variable_1) WHERE table_name_1.column_name_1 = table_name_2.column_name_2 Syntax note: All subqueries must be enclosed in parentheses.

6 98-364 Database Management Fundamentals LESSON 3.1b UNION The UNION clause combines the results of two SQL queries into a single table of all matching rows. The two queries must have the same number of columns and compatible data types to unite. Any duplicate records are removed automatically unless UNION ALL is used. Example: SELECT column_name(s) FROM table_name1 UNION SELECT column_name(s) FROM table_name2 NO duplicates allowed. SELECT column_name(s) FROM table_name1 UNION ALL SELECT column_name(s) FROM table_name2 Duplicates allowed.

7 98-364 Database Management Fundamentals LESSON 3.1b JOIN The INNER JOIN keyword returns rows when there is at least one match in both tables. JOIN returns rows where the value in column_name in table_name1 matches the value in column_name in table_name2. These new rows will have columns from both table_name1 and table_name2, except for column_name. Example: SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name Note: INNER JOIN is the same as JOIN.

8 98-364 Database Management Fundamentals LESSON 3.1b LEFT JOIN The LEFT JOIN keyword returns all rows from the left table ( table_name1 ), even if there are no matches in the right table ( table_name2 ). SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name RIGHT JOIN The RIGHT JOIN keyword returns all rows from the right table ( table_name2 ), even if there are no matches in the left table ( table_name1 ). SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_name Note: In some databases, RIGHT JOIN is called RIGHT OUTER JOIN.

9 98-364 Database Management Fundamentals LESSON 3.1b FULL JOIN The FULL JOIN keyword returns rows when there is a match in one of the tables. The FULL JOIN returns value even if only one of the tables has a value, unlike INNER JOIN, which must have a match in both tables. SELECT column_name(s) FROM table_name1 FULL JOIN table_name2 ON table_name1.column_name=table_name2.column_name

10 98-364 Database Management Fundamentals LESSON 3.1b JOIN Types JOIN —the INNER JOIN keyword returns rows when there is at least one match in both tables. JOIN and INNER JOIN are the same. LEFT JOIN —the LEFT JOIN keyword returns all rows from the left table ( table_name1 ), even if there are no matches in the right table ( table_name2 ). RIGHT JOIN —the RIGHT JOIN keyword returns all rows from the right table ( table_name2 ), even if there are no matches in the left table ( table_name1 ). FULL JOIN —the FULL JOIN keyword returns rows when there is a match in one of the tables.

11 98-364 Database Management Fundamentals LESSON 3.1b INTERSECT INTERSECT combines two or more SELECT statements. INTERSECT is essentially the same as a Boolean AND operation. The SQL INTERSECT operator takes the results of two queries and returns only rows that appear in both result sets. It removes duplicate rows from the final result set unless INTERSECT ALL is used. SELECT * FROM class_info WHERE grade BETWEEN “A” AND “C” INTERSECT SELECT * FROM class_info WHERE grade BETWEEN “B” AND “D” The above INTERSECT query returns all rows from the Class Info table where Grade is between B and C.

12 98-364 Database Management Fundamentals LESSON 3.1b Lesson Review 1. What is a subquery? 2. What is a JOIN ? 3. What is the command that is used to keep duplicates in UNION and INTERSECT commands?


Download ppt "Select Complex Queries 98-364 Database Management Fundamentals LESSON 3.1b."

Similar presentations


Ads by Google