A Tutorial on SQL Nested Queries

A Tutorial on SQL Nested Queries thumbnail
A Tutorial on SQL Nested Queries

The nested SQL query is a form of the SELECT query that is inside another SQL query.
The nested SQL query is also called a subquery. The outside SQL statement is called the parent statement and the inside SQL statement is the nested or subquery. The nested query obtains a result set and the SELECT statement (parent statement) uses this results set for additional processing.

Things You'll Need

  • Computer
  • Database
Show More

Instructions

    • 1

      You can use the subquery for the following purposes:
      - defining a set of row that need to be inserted into a targeted table.
      - defining a results set that will be used to create a view or snapshot.
      - defining one or more values for an update statement.
      - providing values for WHERE, HAVING and START WITH clauses for SELECT, UPDATE and DELETE statements.

    • 2

      The SQL statement obtains information from a table in a particular database. For this example the database name is emp (for employee), the ename is the name of the employee and deptno is department number. You want to obtain all of the employee names in Smith's department. You want to determine in which department 'SMITH' works and then use that answer to list all of the employee's names in that department:

      SELECT ename, deptno
      FROM emp
      WHERE deptno =
      (SELECT deptno
      FROM emp
      WHERE ename = 'SMITH')

    • 3

      The nested query returns the department number (deptno) associated with employee 'SMITH' and the parent query will use the results set for obtaining the name and department number of all employees who work in Smith's department.

      Write your query with the final results in mind and use the nested query to obtain the information necessary for the parent query to return the final results set.

Related Searches:

References

  • Photo Credit business report image by Christopher Hall from Fotolia.com

Comments

You May Also Like

  • Nested SQL Query Tutorial

    Nested SQL statements use SQL queries to retrieve data from within another query. The nested set of queries retrieves rows from tables...

  • SQL Nested Table Tutorial

    Nested SQL queries (also called "sub-queries") provide you with the ability to run a query within a main query. A sub-query returns...

  • Query SQL Tutorial

    Querying an SQL (Structured Query Language) server retrieves data from the database tables. SQL queries are used to select, update, delete or...

  • How to Do a Subselect Query in Access

    In any database application that uses Structured Query Language (SQL) to create queries, the "Select" query is the workhorse. Everything from simple...

  • How to Use Nested Cursors

    Cursors are a structure provided in programmable versions of Structured Query Language (SQL) such as Sybase or Microsoft SQL Server's Transact-SQL. Define...

  • Advanced SQL Queries Tutorial

    Among the more advanced features of SQL are Group By clauses, Aggregate functions, Unions and Subqueries. Having the use of these features...

  • Tutorial for Writing SQL Express Queries

    SQL Server Express is a relational database management system available free from Microsoft. A relational database allows you to store large amounts...

  • How to Create a SQL Query in Microsoft Access

    SQL queries use Structured Query Language (SQL), a standard scripting language, to make requests from databases. You can use four types of...

  • How to Create a Table from Query Results in Microsoft SQL

    Microsoft SQL is a relational model database server that uses the T-SQL and ANSI SQL query languages. The primary function of Microsoft...

  • SQL Statement Definition

    Structured query language (SQL) makes it possible to obtain information fast from millions of records stored in a relational database though query...

  • SQL Select Case Tutorial

    SQL is a powerful data manipulation language. It provides a mechanism for accessing and modifying data stored in relational tables. One feature...

  • How to Use Nested Functions in Excel

    Functions in Microsoft Excel use inputs, which are called arguments, to calculate values, usually referred to as results. The result of a...

  • MySQL Subqueries Tutorial

    Subqueries are queries nested within another query. Subqueries allow you to separate each part of the statement and provide a more readable...

  • SQL Tables Tutorial

    Creating tables in SQL is a key task in constructing a useful database. Taking the time to prepare your database table design...

Related Ads

Featured