SQL SELECT Tutorial for Nested Queries ( Sub Queries )

A Subquery or Inner query or Nested query is a query within another SQL query and embedded within the WHERE clause. A subquery is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved.

Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the operators like =, <, >, >=, <=, IN, BETWEEN etc.

Subqueries are most frequently used with the SELECT statement. The basic syntax is as follows:

SELECT column_name
FROM   table1 
WHERE  column_name OPERATOR
      (SELECT column_name 
      FROM table3 WHERE condition)

Example for Database Structure

In order to demonstrate the usage of HAVING within SELECT statement, we will be using the following database during the course of this tutorial . The database is for an ecommerce store with 5 tables as :
Countries, Orders, Clients, Products, Prodcat

advanced-sql

Questions

1 Find the customers who are from Albania and Algeria?

2 Find the total payments for every customer from Canada?

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *