What is a query?

A query is a request for information or an action from a database or search engine. In plain English, a query is simply a question. In a technical context, it’s a specific command sent to a system to retrieve data or perform a task.
While you might use a query to find information in a search engine like Google, the term most often refers to requests sent to a database.
To communicate with a database, you must write these requests in a specific query language, such as Structured Query Language (SQL).
To help you understand how queries work, we will cover:
- The mechanism behind queries in databases and search engines.
- The different types of queries used to manage data.
- The main languages used to write queries.
- Practical examples of database queries.
For a visual explanation of what a query is, check out this video from Hostinger Academy.
Download glossary for web beginners
How does a query work?
A query works by sending a structured request in a specific language. A system like a database processes the request and returns the needed information or performs an action.
Think of it like ordering at a coffee shop. When you ask for an Americano, you are making a request (a query). The barista understands your language, processes your request (makes the coffee), and gives you the output (your drink).
A database query works the same way: as long as you and the database “speak” the same language, you can request information, and the system provides it.
This process follows a simple flow:
- Request. You write a command in a query language to ask for specific data.
- Process. The database management system (DBMS) interprets your command.
- Output. The system retrieves the data or performs the action and returns a response.
Queries generally fall into two categories:
- A Select query retrieves information, like fetching a list of all customer names from a table.
- An Action query manipulates data, such as adding a new customer, updating an address, or deleting a record.
What are the different types of queries?
The main types of database queries are Select queries, which retrieve data, and Action queries, which modify it. You can also combine these operations to perform more complex tasks.
Here are the most common types and the SQL commands that define them:
- Select query. The most common type of query. It fetches data from one or more tables without changing the data itself.
- Example: Use the SELECT command to get the names and occupations of everyone in a Participant table.
- Action queries. These queries perform operations on data, such as creating, changing, or deleting records.
- Insert query. Adds new records or rows to a table using the INSERT INTO command.
- Update query. Modifies existing records in a table with the UPDATE command and specific criteria.
- Delete query. Removes one or more records from a table with the DELETE command, permanently erasing the data.
- Combined query. This query uses a SELECT statement inside an Action query to identify which records to modify. Developers often achieve this with a subquery in the WHERE clause.
- Example: Use UPDATE to change the status of all employees in a Sales department, where the department ID is first found using a SELECT subquery.
UPDATE Employees SET Status = 'Inactive' WHERE DepartmentID IN (SELECT DepartmentID FROM Departments WHERE DepartmentName = 'Sales');
Query languages
To write a query, you need a query language. While several exist, the most common follow either SQL or NoSQL principles.
SQL is the standard language for relational databases. These databases store data in tables with rows and columns.
It’s important to note the difference between SQL and MySQL: SQL is the query language, while MySQL is a popular database management system that uses SQL.
NoSQL (Not Only SQL) refers to languages designed for non-relational databases. These databases handle data more flexibly and store unstructured formats, such as documents or key-value pairs.
A NoSQL database works well when your data doesn’t fit neatly into tables.
Other specialized query languages include ArangoDB Query Language (AQL), Datalog, and Data Mining Extensions (DMX).
Query examples
Let’s look at some practical SQL query examples. Suppose you have a database table named Participant with the following data:
ID | Name | Sex | Age | Occupation |
1 | John | Male | 17 | Student |
2 | Peter | Male | 26 | Unemployed |
3 | Margareth | Female | 34 | Teacher |
4 | Lea | Female | 34 | Unemployed |
Selecting specific data
To retrieve only the Name and Occupation columns from the Participant table, you would use a SELECT query.
SELECT Name, Occupation FROM Participant;
The result would be a new table with only the requested data:
Name | Occupation |
John | Student |
Peter | Unemployed |
Margareth | Teacher |
Lea | Unemployed |
Deleting data
To remove all participants whose occupation is Unemployed, you would use a DELETE query with a WHERE clause to specify the condition.
DELETE FROM Participant WHERE Occupation = 'Unemployed';
This command removes the matching rows, leaving this result:
ID | Name | Sex | Age | Occupation |
1 | John | Male | 17 | Student |
3 | Margareth | Female | 34 | Teacher |
Inserting new data
To add a new row to the table for a participant named Mario, you would use an INSERT INTO query.
INSERT INTO Participant (ID, Name, Sex, Age, Occupation) VALUES (5, 'Mario', 'Male', 29, 'Plumber');
The Participant table would then include the new record.
ID | Name | Sex | Age | Occupation |
5 | Mario | Male | 29 | Plumber |
Updating existing data
To change Margareth’s occupation to Headmaster, you can use an UPDATE query. The WHERE clause modifies only the correct record.
UPDATE Participant SET Occupation = 'Headmaster' WHERE ID = 3;
The query finds the row with ID = 3 and updates the Occupation field.
ID | Name | Sex | Age | Occupation |
3 | Margareth | Female | 34 | Headmaster |
A note on search engine queries
It’s also worth noting that the term “query” is used differently for search engines. A search engine query is simply the word or phrase you type into the search bar.
Unlike database queries, these don’t require a special language – the search engine’s algorithm processes your natural language to find relevant results.
How do queries work in databases?
Understanding what queries are and how to write them is the first step in managing data. The next step is knowing where systems execute these queries. Most of the time, that place is a DBMS.
A DBMS provides tools to store, manage, and retrieve data using a query language. MySQL is one of the most popular open-source relational database systems in the world.
To see how queries function in real-world applications, we recommend you to learn what MySQL is.
All of the tutorial content on this website is subject to Hostinger's rigorous editorial standards and values.
Comments
June 18 2020
A very great explanation. thank you <3
July 26 2020
very good, thanks.
December 18 2021
A very good explanation Thank much for your wonderful support