SQL Queries and Sub-queries

What is QUERY?

  • A query is an operation that retrieves data from one or more tables or views.
  • SELECT statement can be used for retrieving the data from various tables in a database.
Example:

<Employee> Table

EidEnameAgeCitySalary
E001ABC29Pune20000
E002PQR30Pune30000
E003LMN25Mumbai5000
E004XYZ24Mumbai4000
E005STU32Bangalore25000

1. Selecting all columns (SELECT *)

SELECT * FROM Employee;

2. Displaying particular record with condition (WHERE)

SELECT Ename FROM Employee
WHERE City = 'Pune';
    
Output:

Ename
ABC
PQR

3. SELECT using DISTINCT

DISTINCT clause is used to eliminate the duplicate values from the table.

Example:
SELECT DISTINCT city FROM Employee;

Output:

City
Bangalore
Mumbai
Pune

4. SELECT using IN

'IN' determines whether a specified value matches any value in a sub-query or a list.

Example:
SELECT Eid, Ename FROM Employee
WHERE Salary IN (5000, 20000);

Output:

EidEname
E001ABC
E003LMN

5. SELECT using BETWEEN

'BETWEEN' is used to get those values who fall within a range.

Example:
SELECT Eid, Ename, Salary FROM Employee
WHERE Salary BETWEEN 5000 AND 30000;
   
Output:

EidEnameSalary
E001ABC20000
E002PQR30000
E003LMN5000
E005STU25000

NOT BETWEEN

Example:
SELECT Eid, Ename, Age FROM Employee
WHERE Age NOT BETWEEN 24 AND 25;
   
Output:

EidEnameAge
E001ABC29
E002PQR30
E005STU32

6. SELECT using LIKE

  • LIKE clause is used for comparing a value with similar values using wildcard operators (% and _ ).
  • Suppose, if you want user name starts with 'S', then use 'LIKE' clause as follows,
Example:
SELECT Ename, City, Salary FROM Employee
WHERE Ename LIKE 'S%';

Output:

EidEnameCitySalary
E005STUBangalore25000

LIKE Statements

StatementsDescription
LIKE 'S%'It finds any value which starts with 'S'.
LIKE '%S%'It finds any value which have 'S' in any position.
LIKE '_SS%'It finds any value which have 'SS' in the second and third positions.
LIKE 'S_%_%'It finds any value which starts with 'S' and have at least three characters in length.
LIKE '%S'It finds any value which ends with 'S'.
LIKE '_S%P'It finds any value which have 'S' in the second position and ends with 'P'.
LIKE 'S___P'It finds any value in a five digit numbers which start with 'S' and ends with 'P'.

Note: In the above LIKE statements, instead of 'S' and 'P' you can use any value according to your needs while writing the query. 'S' and 'P' are examples, mentioned for understanding.

6. SELECT using GROUP BY

GROUP BY clause is used to arrange the data into groups.

Example:
SELECT Eid, Ename, City, MAX (Salary) FROM Employee
GROUP BY Salary;

Output:

EidEnameCitySalary
E004XYZMumbai4000
E003LMNMumbai5000
E001ABCPune20000
E005STUBangalore25000
E002PQRPune30000

7. SELECT using ORDER BY

  • ORDER BY clause specifies an order in which to return the rows.
  • It is used to sort the data in ascending or descending order which is based on one or more columns.
Syntax:
SELECT <column-list>
FROM <table_name>
WHERE <condition>
ORDER BY <column1, column2, .. column_n> <ASC | DESC>;

Example:
SELECT Eid, Ename FROM Employee
ORDER BY Ename asc;

Output:

EidEname
E001ABC
E002LMN
E003PQR
E005STU
E004XYZ

8. SELECT using AND, OR and NOT

i. AND
AND requires that two conditions are true.

Syntax:
SELECT <list_of_column_name>
FROM <list_of_table_name>
WHERE <condition1> AND <condition2>;

Example:
SELECT * FROM Employee
WHERE Age= 30 AND City="Pune";

Output:

EidEnameAgeCitySalary
E002PQR30Pune30000

ii. OR
OR requires that one of two conditions is true.

Syntax:
SELECT <list_of_column_name>
FROM <list_of_table_name>
WHERE <condition1> OR <condition2>;

Example:
SELECT * FROM Employee
WHERE City="Pune" OR City="Mumbai";

Output:

EidEnameAgeCitySalary
E001ABC29Pune20000
E002PQR30Pune30000
E003LMN25Mumbai5000
E004XYZ24Mumbai4000

iii. NOT
NOT neglects the specified condition.

Syntax:
SELETE <list_of_column_name>
FROM <list_of_table_name>
WHERE NOT <condition>;

Example:
SELECT * FROM Employee
WHERE NOT City="Pune";

Output:

EidEnameAgeCitySalary
E003LMN25Mumbai5000
E004XYZ24Mumbai4000
E005STU32Bangalore25000

SUB-QUERY

  • Sub-query is a inner query within another query. It is used to return data in the main query as a condition to retrieved the data.
  • Sub-queries are nested SELECT statement.
  • It is a query within a query.
  • Sub-queries are mostly appear within the WHERE or HAVING clause of another SQL statement.
  • It defines with another SELECT statement with a FROM clause and optional WHERE, GROUP BY and HAVING clauses.
  • It produces a single column of data as its result.
  • In a sub-query, ORDER BY clause cannot be specified. ORDER BY clause is specified in the main query.
  • Sub-query is always enclosed in parentheses.
  • It cannot be a UNION, only a single SELECT statement is allowed.
  • In a sub-query, 'SELECT *' cannot be used unless the referring table has only one column and nested query is evaluated first.
Example:
SELECT Ename, Salary FROM Employee
WHERE Salary IN
(SELECT MAX (Salary) FROM Employee);

Output:

EnameSalary
PQR30000
  
Following are the comparison operators where sub-queries are expressed as one SELECT statement connected to another,

Comparison Operator

OperatorDescription
=Equal to
< > or !=Not equal to
>Greater than
<Less than
>=Greater than Equal to
<=Less than Equal to

Multiple-row Comparison Operator

OperatorDescription
INEqual to any value retrieved in an Inner query.
NOT INNot equal to any value retrieved in an inner query.
= ANYEqual to any value retrieved in an inner query – Logical OR.
> ANY, >= ANYRetrieves any highest value.
< ANY, <= ANYRetrieves any smallest value.
= ALLEqual to all values retrieved in an Inner query – Logical AND.
> ALL, >= ALLRetrieves all highest values.
< ALL, <=ALLRetrieves all smallest values.