Professional Documents
Culture Documents
A query is a request for data or information from a database table or combination of tables. This
data may be generated as results returned by Structured Query Language (SQL) or as pictorials,
graphs or complex results, e.g., trend analyses from data-mining tools.
A Subquery or Inner query or a 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 with the SELECT Statement
SELECT job_id,AVG(salary)
FROM employees
GROUP BY job_id
HAVING AVG(salary)<
(SELECT MAX(AVG(min_salary))
FROM jobs
WHERE job_id IN
(SELECT job_id FROM job_history
WHERE department_id
BETWEEN 50 AND 100)
GROUP BY job_id);
Simple Queries:
Selecting all records from a table and displaying all columns:
SELECT EMP_ID
FROM EMPLOYEE_TBL;
Complex query:
AND Query
It might be an AND query. The example below shows that the query is searching for event =
birthday AND paid = yes
OR Query
It might be searching using two parameters for event = lunch OR event = birthday
NOT Query
The parameters might be looking for all events except for birthday.
Query Optimization:
A single query can be executed through different algorithms or re-written in different forms and
structures. Hence, the question of query optimization comes into the picture – Which of these forms
or pathways is the most optimal? The query optimizer attempts to determine the most efficient way
to execute a given query by considering the possible query plans.
Query optimization is a function of many relational database management systems. The query
optimizer attempts to determine the most efficient way to execute a given query by considering the
possible query plans.