Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL/Lesson 4/Slide 1 of 45 Using Subqueries and Managing Databases Objectives In this lesson, you will learn to: *Use subqueries * Use subqueries with.

Similar presentations


Presentation on theme: "SQL/Lesson 4/Slide 1 of 45 Using Subqueries and Managing Databases Objectives In this lesson, you will learn to: *Use subqueries * Use subqueries with."— Presentation transcript:

1 SQL/Lesson 4/Slide 1 of 45 Using Subqueries and Managing Databases Objectives In this lesson, you will learn to: *Use subqueries * Use subqueries with the IN clause * Use subqueries with the EXISTS clause * Use nested subqueries * Use correlated subqueries * Use the SELECT INTO statement * Use the UNION operator * View, rename, and delete databases

2 SQL/Lesson 4/Slide 2 of 45 Using Subqueries and Managing Databases Subqueries * A subquery can be defined as a SELECT query that returns a single value * Subqueries are nested within a SELECT, INSERT, UPDATE, or DELETE statement * Subqueries can be used to retrieve data from multiple tables and can be used as an alternative to a join * Subqueries can also be used inside the WHERE or HAVING clause of the SELECT, INSERT, UPDATE, and DELETE statements

3 SQL/Lesson 4/Slide 3 of 45 Using Subqueries and Managing Databases 4.D.1 Using one Query in Another *List the contract recruiters who live in the same city as the external candidate Barbara Johnson.

4 SQL/Lesson 4/Slide 4 of 45 Using Subqueries and Managing Databases Task List *Create a format for the query output *Identify the components of the query *Execute the query *Verify that the query output is as per the required results

5 SQL/Lesson 4/Slide 5 of 45 Using Subqueries and Managing Databases Create a format for the query output *Result 3 The required output from the query is the names of the contract recruiters who reside in the same city as 'Barbara Johnson' 3The required data is present in the ContractRecruiter and ExternalCandidate tables

6 SQL/Lesson 4/Slide 6 of 45 Using Subqueries and Managing Databases Draft the query *Result 3The required information is available in the ExternalCandidate and ContractRecruiter tables 3Therefore, the query using the SELECT statement should be: SELECT cName FROM ContractRecruiter WHERE cCity = (SELECT cCity FROM ExternalCandidate WHERE vFirstName = 'Barbara' AND vLastName = 'Johnson')

7 SQL/Lesson 4/Slide 7 of 45 Using Subqueries and Managing Databases Execute the query *Action:  In the Query Analyzer window, type the query 3Execute the query

8 SQL/Lesson 4/Slide 8 of 45 Using Subqueries and Managing Databases Verify that the query output is as per the required results * Action: 3Check whether: ä The required rows are displayed

9 SQL/Lesson 4/Slide 9 of 45 Using Subqueries and Managing Databases More on Subqueries *Subqueries with IN 3 A subquery introduced with IN returns zero or more values 3 Example SELECT Au_Id FROM TitleAuthor WHERE Title_Id IN (SELECT Title_Id FROM Sales)

10 SQL/Lesson 4/Slide 10 of 45 Using Subqueries and Managing Databases More on Subqueries (Contd.) *Subqueries with EXISTS 3 A subquery, when used with the EXISTS clause, always returns data in terms of a TRUE or FALSE value 3 Example SELECT Pub_Name FROM Publishers WHERE EXISTS (SELECT * FROM Titles WHERE Type = 'business')

11 SQL/Lesson 4/Slide 11 of 45 Using Subqueries and Managing Databases More on Subqueries (Contd.) *Subqueries with Aggregate Functions 3 Aggregate functions can also be used in subqueries 3 Example SELECT Title FROM Titles WHERE Advance > (SELECT AVG(Advance) FROM Titles WHERE Type = 'business')

12 SQL/Lesson 4/Slide 12 of 45 Using Subqueries and Managing Databases More on Subqueries (Contd.) *Subqueries Restrictions 3SQL Server restricts the use of certain methods and techniques, and forces the implementation of certain standards while using subqueries. The restrictions imposed are: ä The column list of the SELECT statement of a subquery introduced with a comparison operator can include only one column ä The column used in the WHERE clause of the outer query should be compatible with the column used in the SELECT list of the inner query

13 SQL/Lesson 4/Slide 13 of 45 Using Subqueries and Managing Databases More on Subqueries (Contd.) ä The ORDER BY clause and the GROUP BY clause cannot be used in the inner query when =, !=,, or >= are used in the main query, as the inner query may return more than one value that cannot be handled by the outer query

14 SQL/Lesson 4/Slide 14 of 45 Using Subqueries and Managing Databases More on Subqueries (Contd.) * Nested Subqueries 3A subquery can itself contain one or more subqueries 3Example SELECT 'Author Name' = SUBSTRING (Au_Fname, 1, 1) + '. '+ Au_Lname FROM Authors WHERE Au_Id IN (SELECT Au_Id FROM TitleAuthor WHERE Title_Id =(SELECT Title_Id FROM Titles WHERE Title = 'Net Etiquette'))

15 SQL/Lesson 4/Slide 15 of 45 Using Subqueries and Managing Databases More on Subqueries (Contd.) * Correlated Subqueries 3 Can be defined as queries that depend on the outer query for its evaluation 3Example SELECT Title, Type, Advance FROM Titles t1 WHERE t1.Advance > (SELECT AVG(t2.Advance) FROM Titles t2 WHERE t1.Type = t2.Type)

16 SQL/Lesson 4/Slide 16 of 45 Using Subqueries and Managing Databases More on Subqueries (Contd.) * Queries With Modified Comparison Operators 3 SQL Server provides the ALL and ANY keywords that can be used to modify the existing comparison operator

17 SQL/Lesson 4/Slide 17 of 45 Using Subqueries and Managing Databases 4.D.2 Extracting Data Into Another Table * To carry out an analysis of the profile of candidates who have applied for recruitment in May 2001, you need to copy their details into a new table.

18 SQL/Lesson 4/Slide 18 of 45 Using Subqueries and Managing Databases Task List *Identify the output requirements of the query *Draft the query *Execute the query *Verify that the query output is as per the required results

19 SQL/Lesson 4/Slide 19 of 45 Using Subqueries and Managing Databases Identify the output requirements of the query *Result: 3 The required output from the query is the transfer of data from the ExternalCandidate table to a temporary table called tempExternalCandidate

20 SQL/Lesson 4/Slide 20 of 45 Using Subqueries and Managing Databases Draft the query *SELECT INTO Statement 3A SELECT statement with the INTO clause is used to store the result set in a new table without a data definition process. The SELECT INTO statement creates a new table. 3Syntax SELECT columns_list INTO new_table_name FROM table_name1, table_name2,………, table_name n WHERE condition1, condition2,………., condition n

21 SQL/Lesson 4/Slide 21 of 45 Using Subqueries and Managing Databases Draft the query (Contd.) 3 Example SELECT Title_Id, Title INTO NewTitles FROM Titles WHERE Price > $15 *Result: 3The required information is available in the ExternalCandidate table 3Therefore, the query using the SELECT statement should be:

22 SQL/Lesson 4/Slide 22 of 45 Using Subqueries and Managing Databases Draft the query (Contd.) SELECT * INTO tempExternalCandidate FROM ExternalCandidate WHERE DATEPART(mm,dDateOfApplication)= 5 AND DATEPART(yyyy,dDateOfApplication)= 2001

23 SQL/Lesson 4/Slide 23 of 45 Using Subqueries and Managing Databases Execute the query *Action  In the Query Analyzer window, type the query 3 Execute the query

24 SQL/Lesson 4/Slide 24 of 45 Using Subqueries and Managing Databases Verify that the query output is as per the required results * Action: 3 Check whether: ä The target table has all data from the source table Just a Minute… * Write a query to copy all the contents of InternalCandidate to a table called backupInternalCandidate.

25 SQL/Lesson 4/Slide 25 of 45 Using Subqueries and Managing Databases 4.D.3 Combining Data From Two Tables * A list of contract recruiters and recruitment agencies along with their phone numbers is required.

26 SQL/Lesson 4/Slide 26 of 45 Using Subqueries and Managing Databases Task List *Create a format for the query output *Draft the query *Execute the query *Verify that the query output is as per the required results

27 SQL/Lesson 4/Slide 27 of 45 Using Subqueries and Managing Databases Create a format for the query output *Result: 3 The required output from the query is a single list of names and phone numbers of contract recruiters and recruitment agencies

28 SQL/Lesson 4/Slide 28 of 45 Using Subqueries and Managing Databases Draft the query *UNION Operator 3Is used to combine the result set of two or more queries 3Syntax SELECT column_list [INTO new_table_name] [FROM clause] [WHERE clause] [GROUP BY clause][HAVING clause] [UNION [ALL] SELECT column_list [FROM clause] [WHERE clause] [GROUP BY clause][HAVING clause]...] [ORDER BY clause] [COMPUTE clause]

29 SQL/Lesson 4/Slide 29 of 45 Using Subqueries and Managing Databases Draft the query (Contd.)  Result 3 The required information is available in the ContractRecruiter and RecruitmentAgencies tables 3 Therefore, the query using the SELECT statement should be: SELECT cName,cPhone FROM ContractRecruiter UNION SELECT cName, cPhone FROM RecruitmentAgencies

30 SQL/Lesson 4/Slide 30 of 45 Using Subqueries and Managing Databases Execute the query *Action  In the Query Analyzer window, type the query 3 Execute the query

31 SQL/Lesson 4/Slide 31 of 45 Using Subqueries and Managing Databases Verify that the query output is as per the required results * Action: 3Check whether: ä All the required columns are displayed ä All rows from both tables are displayed as one list Just a Minute… * Display the list of college names, newspaper names, and their addresses in the following format: Name Address

32 SQL/Lesson 4/Slide 32 of 45 Using Subqueries and Managing Databases Databases * A database is a collection of tables and objects such as views, indexes, stored procedures, and triggers * System Databases 3 master Database-records all the server-specific configuration information, including authorized users, databases, system configuration settings, and remote servers 3 tempdb Database-is a temporary database that is used as an interim storage area 3 model Database-acts as a template or a prototype for the new databases 3 msdb Database-supports SQL Server Agent

33 SQL/Lesson 4/Slide 33 of 45 Using Subqueries and Managing Databases Databases (Contd.) * System Tables 3 Are a set of tables that are used by SQL server to store information about configuration, security, and object information 3 SQL Server manages each database with the help of the system tables, which contain all the system information Just a Minute… What is the purpose of model database in SQL Server?

34 SQL/Lesson 4/Slide 34 of 45 Using Subqueries and Managing Databases Databases (Contd.) * Files: The three types of files that a database has are: ä Primary ä Secondary ä Transaction Log * Filegroup: Is a collection of files * A file or filegroup cannot be used by more than one database * A file can be a member of only one filegroup

35 SQL/Lesson 4/Slide 35 of 45 Using Subqueries and Managing Databases Viewing a Database * The information regarding the database such as owner, size, date of creation, and status can be viewed using the following: sp_helpdb database_name

36 SQL/Lesson 4/Slide 36 of 45 Using Subqueries and Managing Databases Renaming a Database * The name of a database can be changed using the sp_renamedb command. The database should not be in use when it is being renamed, and it should be set to the single- user mode 3 Syntax sp_renamedb 'old_name', 'new_name'

37 SQL/Lesson 4/Slide 37 of 45 Using Subqueries and Managing Databases Deleting a Database * The DROP DATABASE statement is used to delete a database 3 Syntax DROP DATABASE database_name Just a Minute… * List the three types of operating system files that store the data and objects of SQL Server database.

38 SQL/Lesson 4/Slide 38 of 45 Using Subqueries and Managing Databases Summary In this lesson, you learned that: *Subqueries are nested within a SELECT, INSERT, UPDATE, or DELETE statement * A subquery can be used inside the WHERE or HAVING clauses of the outer SELECT, INSERT, UPDATE, or DELETE statements *The subquery introduced with IN or NOT IN returns zero or more values * The subquery used with the EXISTS clause returns data in terms of TRUE or FALSE

39 SQL/Lesson 4/Slide 39 of 45 Using Subqueries and Managing Databases Summary (Contd.) * A subquery can contain one or more subqueries. There is no restriction on the number of subqueries one can include with the SELECT, INSERT, UPDATE, or DELETE statements * A correlated subquery can be defined as a query that depends on the outer query for its evaluation * A SELECT statement with an INTO clause can be used to store the result set in a new table without any data definition process * The UNION operator is used to combine the result set of two or more queries into one

40 SQL/Lesson 4/Slide 40 of 45 Using Subqueries and Managing Databases Summary (Contd.) * By default the result set of a UNION operator removes the duplicate rows from the queries combined, unless an ALL clause is specified with the UNION operator * A database consists of a collection of tables with data and other objects such as views, indexes, stored procedures, and triggers *SQL Server has the following system databases: 3master 3tempdb 3model 3msdb

41 SQL/Lesson 4/Slide 41 of 45 Using Subqueries and Managing Databases Summary (Contd.) *SQL Server stores its configuration, security, and object information in tables called system tables *A database consists of the following types of files: 3Primary data file 3Secondary data file 3Transaction log file *A filegroup is a collection of files. Filegroups allow files to be grouped together *A database comprises of a primary filegroup and any user- defined filegroup(s)

42 SQL/Lesson 4/Slide 42 of 45 Using Subqueries and Managing Databases Summary (Contd.) * Creation of a database involves determining the name of the database, the size of the database, and the files used to store data in the database * sp_helpdb is used to view the information regarding a database * sp_renamedb is used to rename a database * The DROP DATABASE statement is used to delete a database


Download ppt "SQL/Lesson 4/Slide 1 of 45 Using Subqueries and Managing Databases Objectives In this lesson, you will learn to: *Use subqueries * Use subqueries with."

Similar presentations


Ads by Google