Which clause can appear only once at the end of the query containing compound SELECT statements?

Last update on August 19 2022 21:51:36 (UTC/GMT +8 hours)

What is subquery in SQL?

A subquery is a SQL query nested inside a larger query.

  • A subquery may occur in :
    • - A SELECT clause
    • - A FROM clause
    • - A WHERE clause
  • The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE statement or inside another subquery.
  • A subquery is usually added within the WHERE Clause of another SQL SELECT statement.
  • You can use the comparison operators, such as >, <, or =. The comparison operator can also be a multiple-row operator, such as IN, ANY, or ALL.
  • A subquery is also called an inner query or inner select, while the statement containing a subquery is also called an outer query or outer select.
  • The inner query executes first before its parent query so that the results of an inner query can be passed to the outer query.

You can use a subquery in a SELECT, INSERT, DELETE, or UPDATE statement to perform the following tasks:

  • Compare an expression to the result of the query.
  • Determine if an expression is included in the results of the query.
  • Check whether the query selects any rows.

Syntax :

Which clause can appear only once at the end of the query containing compound SELECT statements?

  • The subquery (inner query) executes once before the main query (outer query) executes.
  • The main query (outer query) use the subquery result.

SQL Subqueries Example :

In this section, you will learn the requirements of using subqueries. We have the following two tables 'student' and 'marks' with common field 'StudentID'.

Which clause can appear only once at the end of the query containing compound SELECT statements?

         
Which clause can appear only once at the end of the query containing compound SELECT statements?

            student                                        marks

Now we want to write a query to identify all students who get better marks than that of the student who's StudentID is 'V002', but we do not know the marks of 'V002'.
- To solve the problem, we require two queries. One query returns the marks (stored in Total_marks field) of 'V002' and a second query identifies the students who get better marks than the result of the first query.

First query:

SELECT *  FROM `marks`  WHERE studentid = 'V002';

Query result:

Which clause can appear only once at the end of the query containing compound SELECT statements?

The result of the query is 80.
- Using the result of this query, here we have written another query to identify the students who get better marks than 80. Here is the query :

Second query:

SELECT a.studentid, a.name, b.total_marks FROM student a, marks b WHERE a.studentid = b.studentid AND b.total_marks >80;

Relational Algebra Expression:

Which clause can appear only once at the end of the query containing compound SELECT statements?

Relational Algebra Tree:

Which clause can appear only once at the end of the query containing compound SELECT statements?

Query result:

Which clause can appear only once at the end of the query containing compound SELECT statements?

Above two queries identified students who get the better number than the student who's StudentID is 'V002' (Abhay).

You can combine the above two queries by placing one query inside the other. The subquery (also called the 'inner query') is the query inside the parentheses. See the following code and query result :

SQL Code:

SELECT a.studentid, a.name, b.total_marks FROM student a, marks b WHERE a.studentid = b.studentid AND b.total_marks > (SELECT total_marks FROM marks WHERE studentid = 'V002');

Query result:

Which clause can appear only once at the end of the query containing compound SELECT statements?

Pictorial Presentation of SQL Subquery:

Which clause can appear only once at the end of the query containing compound SELECT statements?

Subqueries: General Rules

A subquery SELECT statement is almost similar to the SELECT statement and it is used to begin a regular or outer query. Here is the syntax of a subquery:

Syntax:

(SELECT [DISTINCT] subquery_select_argument FROM {table_name | view_name} {table_name | view_name} ... [WHERE search_conditions] [GROUP BY aggregate_expression [, aggregate_expression] ...] [HAVING search_conditions])

Subqueries: Guidelines

There are some guidelines to consider when using subqueries :

  • A subquery must be enclosed in parentheses. 
  • A subquery must be placed on the right side of the comparison operator. 
  • Subqueries cannot manipulate their results internally, therefore ORDER BY clause cannot be added into a subquery. You can use an ORDER BY clause in the main SELECT statement (outer query) which will be the last clause.
  • Use single-row operators with single-row subqueries. 
  • If a subquery (inner query) returns a null value to the outer query, the outer query will not return any rows when using certain comparison operators in a WHERE clause.

Type of Subqueries

  • Single row subquery : Returns zero or one row.
  • Multiple row subquery : Returns one or more rows.
  • Multiple column subqueries : Returns one or more columns.
  • Correlated subqueries : Reference one or more columns in the outer SQL statement. The subquery is known as a correlated subquery because the subquery is related to the outer SQL statement.
  • Nested subqueries : Subqueries are placed within another subquery.

In the next session, we have thoroughly discussed the above topics. Apart from the above type of subqueries, you can use a subquery inside INSERT, UPDATE and DELETE statement. Here is a brief discussion :

Subqueries with INSERT statement

INSERT statement can be used with subqueries. Here are the syntax and an example of subqueries using INSERT statement.

Syntax:

INSERT INTO table_name [ (column1 [, column2 ]) ] SELECT [ *|column1 [, column2 ] FROM table1 [, table2 ] [ WHERE VALUE OPERATOR ];

If we want to insert those orders from 'orders' table which have the advance_amount 2000 or 5000 into 'neworder' table the following SQL can be used:

Sample table: orders

SQL Code:

INSERT INTO neworder SELECT * FROM orders WHERE advance_amount in(2000,5000);

Output:

Which clause can appear only once at the end of the query containing compound SELECT statements?

To see more details of subqueries using INSERT statement click here.

Subqueries with UPDATE statement

In a UPDATE statement, you can set new column value equal to the result returned by a single row subquery. Here are the syntax and an example of subqueries using UPDATE statement.

Syntax:

UPDATE table SET column_name = new_value [ WHERE OPERATOR [ VALUE ] (SELECT COLUMN_NAME FROM TABLE_NAME) [ WHERE) ]

If we want to update that ord_date in 'neworder' table with '15-JAN-10' which have the difference of ord_amount and advance_amount is less than the minimum ord_amount of 'orders' table the following SQL can be used:

Sample table: neworder

SQL Code:

UPDATE neworder SET ord_date='15-JAN-10' WHERE ord_amount-advance_amount< (SELECT MIN(ord_amount) FROM orders);

Output:

Which clause can appear only once at the end of the query containing compound SELECT statements?

To see more details of subqueries using UPDATE statement click here.

Subqueries with DELETE statement

DELETE statement can be used with subqueries. Here are the syntax and an example of subqueries using DELETE statement.

Syntax:

DELETE FROM TABLE_NAME [ WHERE OPERATOR [ VALUE ] (SELECT COLUMN_NAME FROM TABLE_NAME) [ WHERE) ]

If we want to delete those orders from 'neworder' table which advance_amount are less than the maximum advance_amount of 'orders' table, the following SQL can be used:

Sample table: neworder

SQL Code:

DELETE FROM neworder WHERE advance_amount< (SELECT MAX(advance_amount) FROM orders);

Output:

Which clause can appear only once at the end of the query containing compound SELECT statements?

To see more details of subqueries using DELETE statement click here.

What Next?

  • SQL Subqueries - Slide Presentation
  • Single Row Subqueries
  • Multiple Row and Column Subqueries
  • Correlated subqueries using aliases
  • Nested subqueries

Note : Outputs of the said SQL statement shown here is taken by using Oracle Database 10g Express Edition.

Practice SQL Exercises

  • SQL Exercises, Practice, Solution
  • SQL Retrieve data from tables [33 Exercises]
  • SQL Boolean and Relational operators [12 Exercises]
  • SQL Wildcard and Special operators [22 Exercises]
  • SQL Aggregate Functions [25 Exercises]
  • SQL Formatting query output [10 Exercises]
  • SQL Quering on Multiple Tables [8 Exercises]
  • FILTERING and SORTING on HR Database [38 Exercises]
  • SQL JOINS
    • SQL JOINS [29 Exercises]
    • SQL JOINS on HR Database [27 Exercises]
  • SQL SUBQUERIES
    • SQL SUBQUERIES [39 Exercises]
    • SQL SUBQUERIES on HR Database [55 Exercises]
  • SQL Union[9 Exercises]
  • SQL View[16 Exercises]
  • SQL User Account Management [16 Exercise]
  • Movie Database
    • BASIC queries on movie Database [10 Exercises]
    • SUBQUERIES on movie Database [16 Exercises]
    • JOINS on movie Database [24 Exercises]
  • Soccer Database
    • Introduction
    • BASIC queries on soccer Database [29 Exercises]
    • SUBQUERIES on soccer Database [33 Exercises]
    • JOINS queries on soccer Database [61 Exercises]
  • Hospital Database
    • Introduction
    • BASIC, SUBQUERIES, and JOINS [39 Exercises]
  • Employee Database
    • BASIC queries on employee Database [115 Exercises]
    • SUBQUERIES on employee Database [77 Exercises]
  • More to come!

Want to improve the above article? Contribute your Notes/Comments/Examples through Disqus.

Previous: Using a where clause to join tables based on nonkey columns
Next: Single Row Subqueries

Which clause is used at the end of the SELECT statement?

The USE clause sets the database that we will be querying. You typically have more than one database on your database server. You have to specify which database you are working in. The semicolon “;” indicates the end of a statement.

Which clause is used with SELECT statement?

Each SQL query statement must contain both a SELECT and a FROM clause. The combination of these two clauses determine the table columns that are searched by the query. The WHERE clause and other advanced clauses further limit data retrieval to specific table rows.

Which clause would you use in a SELECT statement to limit the display to those?

Answer: A. The WHERE clause is an optional clause in the SELECT query which can be used only once to restrict the number of rows.

Which of the following can be used in a SELECT statement to restrict a result

The LIMIT clause can restrict the result set of the query to some maximum number of rows.