Back to articles list Articles Cookbook
7 minutes read

What Is an SQL Query?

An SQL query is the most basic SQL command. It is a question you ask a database. In this article, we dive into the basics of SQL queries to kickstart your SQL journey with practical knowledge right away!

SQL, or Structured Query Language, is a programming language designed to interact with databases. When you want to access data in a database, be it to alter, delete, add, or simply extract information, you use SQL.

SQL can work and interact with large amounts of data. Its syntax is sleek and straightforward. If you want to know more about SQL, its history, and its basics, read our article that addresses this exact question: "What Is SQL?"

For those who want to learn SQL by practicing, we recommend our SQL Basics course which you can start even right after reading this article. This fully interactive course contains the essential foundations of SQL in 129 exercises!

SQL Query: A Question to the Database

An SQL query is a command used to get data out of a database. It is a flexible instrument for accessing the needed data.

An SQL query is essentially a question a user asks a database. The questions may vary in complexity, from "which car models are being sold?" to "how many Volvo cars on average are sold to Mr. Smith's business each year?"

In this article, we look at some simpler examples of SQL queries, that is, simpler questions you can ask a database. Before diving in further, our beginner's guide to SQL terms may be quite useful to understand some things that are coming!

Tables in SQL Databases

SQL databases store information in tables. Each table has a name that usually tells us what kind of information it stores: a table named students probably stores information about students, one named course probably stores information about courses, etc.

Each table consists of columns and rows. Columns build the structure of a table. Each column has a name that tells us what information it stores. It also has a data type that tells us whether the information in the column is text, number, date and time, etc. Rows contain the data itself, defined by columns.

Let's look at an example: the table students. It contains information about the students at a university. It has 5 columns. Here's what each column means:

  • id – the unique ID of the student.
  • name – the student's first and last names.
  • Department – the department in the university to which the student belongs.
  • avg_rating – the student's average grade (from 1 to 5).
  • current_year – the student's current year.

The columns name and department store string values, id and current_year are integers, and avg_rating is a floating-point number:

idnamedepartmentavg_ratingcurrent_year
1Tracy CarpenterComputer Science4.41
2Kornelia HoldingComputer Science3.671
3Will ParkerIT Management4.053
4Daria HendersonIT Management4.74
5James MartinsWorld History2.84
6Kim YuNew Media Arts3.963

Example Questions to Ask

Let's think of some examples of questions/queries we can ask this table:

  • Who are all the students that study at the university?
  • What are the names of the students who study at the university?
  • What are the names of the students who have an average rating of at least 4?
  • Which students are in the first year in the Computer Science department?
  • And so on.

The number of possible queries with just this one table is virtually endless. They all share a common structure:

SELECT …
FROM students
…

As you see, we use the name of our table, students, in a query with the keyword FROM.

How Do You Write an SQL Query?

In this article, we focus on the most basic parts of any SQL query: SELECT, FROM, and WHERE. Let's get some data from the students table using these keywords.

Example SQL Query 1: Extract all Information From a Table (Using SELECT * in Queries)

SELECT *
FROM students;

This is the most basic type of query. It selects all columns (by using the * symbol after SELECT) and all rows (by not having any filter) from the table. In natural language, this query asks: "give me all the information on all students."

Here's what the result looks like:

idnamedepartmentavg_ratingcurrent_year
1Tracy CarpenterComputer Science4.41
2Kornelia HoldingComputer Science3.671
3Will ParkerIT Management4.053
4Daria HendersonIT Management4.74
5James MartinsWorld History2.84
6Kim YuNew Media Arts3.963

As you see, this is an entire table we have seen previously. Let's look at another example.

Example SQL Query 2: Extract the Students' Names, Their Departments, and Their Current Years (Selecting Specific Columns)

SELECT name, department, current_year
FROM students;

This query has a little bit more going on. It selects all rows (again, no filter included) but not all columns – only name, department, and current_year. The query says: "give me the names of the students, their departments, and their years."

Here's what the result looks like:

namedepartmentcurrent_year
Tracy CarpenterComputer Science1
Kornelia HoldingComputer Science1
Will ParkerIT Management3
Daria HendersonIT Management4
James MartinsWorld History4
Kim YuNew Media Arts3

It's the same table as before but with fewer columns. Let's look at an even more advanced example.

Example SQL Query 3: Extract the Names of the IT Management Students (Filtering Queries Using WHERE)

SELECT name
FROM students
WHERE department = 'IT Management';

This query not only selects just one column, name, but also adds a filter for rows. It only selects rows whose department value is equal to 'IT Management'. This query asks: "give me the names of the students who study in the IT Management department." And here's the result:

name
Will Parker
Daria Henderson

Example SQL Query 4: Extract the IDs and the Names of the Fourth-Year IT Management Students (Multiple Filters in Queries Using AND)

Often, you want to include several filter conditions. No problem! Add as many conditions as you want using the AND and the OR keywords. If you have two conditions tied together with the AND keyword, the query searches for rows that fulfill both conditions. If they are with OR, the query searches for rows that fulfill either condition.

Let's see an example:

SELECT id, name
FROM students
WHERE department = 'IT Management'
AND current_year = 4;

The query asks: "give me the IDs and the names of the IT Management students who are in their fourth year." Here is the result:

idname
4Daria Henderson

And now, here is an example of filter conditions with OR.

Example SQL Query 5: Extract the Names and the Departments of the Students in IT Management or World History (Multiple Filters in Queries Using OR)

SELECT name, department
FROM students
WHERE department = 'IT Management'
OR department = 'World History';

This query asks: "give me the names and the departments of the students who are either in IT Management or World History." Here's what the result looks like:

namedepartment
Will ParkerIT Management
Daria HendersonIT Management
James MartinsWorld History

SQL Queries: General Structure

Let's summarize and look at the general structure of an SQL query. The general structure of an SQL query is as follows:

SELECT …
FROM …
WHERE …
GROUP BY …
HAVING …
ORDER BY …;

Of all these keywords, only two are mandatory: SELECT and FROM – the ones you already know. Let's now go through each keyword:

  • SELECT is followed by the columns you want to get from the table (e.g., SELECT name, department, current_year) or the * symbol to select all columns.
  • FROM is followed by the name of the table from which you want to get information.
  • WHERE is followed by one or more filter conditions for the data (e.g., WHERE current_year = 1).
  • GROUP BY is followed by one or more columns by which you want to group the data (e.g., GROUP BY department aggregates students by their departments).
  • HAVING is followed by a condition that filters groups of data. This keyword is always used together with GROUP BY.
  • ORDER BY is followed by one or more columns by which you want to sort the data.

Learn All About SQL Queries With Our "SQL Basics" Course!

You can do much more in queries than what we have covered in this article, such as grouping data with GROUP BY, sorting results with ORDER BY, and even querying multiple tables at once using various JOINs! Our interactive SQL Basics course provides you with the theory and practice on all of these and much more in a fun and interactive way. I invite you to try it!