AfterAcademy Tech
•
22 Jan 2020

In this blog, we will learn what query and subquery are? These terms are normally used in the DBMS. So, let's get started.
In simple terms, we can explain query as questions or doubts. Query means finding information, asking questions about something, especially in order to express one's doubts about it or to check its validity or accuracy.
A Query is used to traverse over some data may be of small or large quantity to find the needed information.
Data is the facts and figures stored digitally or physically at a location. The data provides us with the base on which we run or perform our query. Books in a library can be considered as data. Information on the web can be considered as data. To look for the desired information in this heap of data is called a query.
We perform many queries in our day to day life. Asking questions is a form of query. Searching info on the web is a form of query. Looking for places on Map is a form of query. In each of these examples, we are looking for the required information in different ways. For asking questions we provide query in the form of voice while searching the web we enter keywords while looking for places on the map we enter the location. This is called a query language. A query language is a language which is used to specify our need in the retrieval procedures of information.
A database also contains a lot of data. It stores data in the form of tables. This form of data is used in many places because of its various benefits. A database query refers to the request of data or information from a database. The database consists of tables which contain records of various entities. Thus a database query is used to query the records of a table. A database query is performed using a database query language which generates data of different types according to function. A number of query language have been developed for different database engines and purposes, one such language is SQL which is the most widely used and well-known database query language which generates result in the form of rows and columns.
A database query can be of two types :
SELECT Query: A select query is used to retrieve data from a single table of a combination of multiple tables.
SQL uses a SELECT statement to select, or extract, specific data based on the given base condition.
ACTION Query: The action query is used to perform operations on the database such as insertion, deletion, modification, alteration, etc.
This query changes the database in one way or another.
Example of database query:
Let's take an example of a database Company which consists of a table Employees. This "Employees" table stores the Emp_Id and Emp_Name of all the Employees of an Organization.
If we want to look at the data of the table Employees we need to run a database query which will return the data in a readable format.
To get the Employee's Emp_Id and Emp_Name of all the employees the SQL Query is:
SELECT Emp_Id,Emp_Name FROM Employees;
We can further filter the results with a WHERE clause. WHERE Clause is used to specify the conditions or properties of the data we are interested in.
To get the details of Employees whose Emp_Id is greater than 100 the SQL Query will be :
SELECT Emp_Id,Emp_Name FROM Employees WHERE Emp_Id > 100;
Other examples of Queries in SQL :
INSERT INTO Employees(Emp_Id,Emp_Name,Salary) VALUES(7,"Ravi",20000)
>>> Inserts data into Employees table.
UPDATE Employees SET Name="Ravi Kumar" WHERE Emp_Id=7
>>> Updates name of Employees to Ravi Kumar where Emp_Id = 7.
DELETE FROM Employees WHERE Emp_id=9;
>>> Delete details of Employees whose Emp_id is 9.
Different task can be performed with the help of queries and the primary purpose is to get information from the database according to the needs specified in the query.
With the help of queries, we can easily retrieve the stored information and perform time-consuming tasks in the database in a few seconds on the basis of our conditions.
Using the query saves us a lot of time because we don't see the information by going to all the rows and columns of each table of the database to retrieve information, we run a query statement and information in front of us. Queries also can perform calculations on our data or automate data management tasks.
This is all about a query. Now, let's learn about SubQuery.
A Subquery is a type of query which is written inside another query. A subquery becomes a part of a larger query. A subquery is also called INNER QUERY OR NESTED QUERY. A subquery provides data to the main query also called the parent query or outer query.
A subquery is basically a SELECT statement that is embedded in a clause of another SQL statement. A subquery can be placed in:
of the parent query or outer query.
The inner query is executed once before its parent query so that the results of an inner query can be passed to the outer query.
Syntax:
SELECT select_list
FROM table
WHERE expr OPERATOR (SELECT select_list FROM table);
Single row subquery
SELECT Emp_id,Emp_Name FROM Employees WHERE Emp_Name = (SELECT Emp_Name FROM Employees WHERE Emp_Id = 7);
Multiple row subquery
SELECT Emp_id,Emp_Name FROM Employees
WHERE Salary IN (SELECT MIN(Salary) FROM Employees GROUP BY(Dept_Id));
Multiple column subquery
SELECT Emp_id,Emp_Name FROM Employees
WHERE (Salary,Name) IN (SELECT Salary,Name FROM Employees WHERE Emp_Id = 7);
Correlated subquery
SELECT Emp_Id,Emp_name FROM Employees emp
WHERE Salary > (SELECT avg(Salary) FROM Employees WHERE Dept_id = emp.Dept_id);
The subquery can be used inside a SELECT, INSERT, UPDATE or DELETE statement or inside another subquery. It can be used to perform the following tasks:
That's all about Queries and SubQueries. Hope you learned something new today.
Do share this blog with your friends to spread the knowledge. Visit our YouTube channel for more content. You can read more blogs from here.
Keep Learning 🙂
Team AfterAcademy!
AfterAcademy Tech
In this blog, we will learn how to cross-join two tables in DBMS, and what are the benefits of doing it with example.

AfterAcademy Tech
In this blog, we will learn what is a stored procedure in DBMS, and how it is executed depending on the number of parameters passed.

AfterAcademy Tech
In this blog, we will learn what is RDBMS and how it is different from DBMS. People often use these words intechangeably. But there is a difference between these two terms. So, let's learn how.

AfterAcademy Tech
In this blog, we will learn how to join two tables in DBMS. We will also learn about the various types of joins, mainly the inner and the outer join.
