6th Feb 2025 5 minutes read SQL Query Syntax Ekre Ceannmor SQL Basics Table of Contents What Is SQL? What Is a Query? Basic SQL SELECT Syntax Get All Data from the Table Get Some Columns from the Table Get Specific Rows from the Table Sort the Result Want to Learn More SQL Query Syntax? Looking for a quick overview of SQL query syntax? In this article, we will discuss the building blocks of a simple SQL query. Are you starting to learn SQL and wondering about the proper way to structure an SQL query? In this article, we will discuss what a query is and what is the proper way to construct one. We’ll also include some helpful examples. Ready? Let’s get started with a very basic question: What Is SQL? Structured Query Language (SQL) is a programming language designed for interacting with databases. It is the primary language used to operate on large amounts of data. SQL is written in a similar way to English, so most people find the syntax quite understandable. If you want to learn SQL through interactive practice, check out our SQL Basics course. It includes more than 100 exercises that you can do right in your browser! What Is a Query? The word “query” is synonymous to “question” or a request for information. In the context of SQL, a query is a statement that we use to make a request to a database for some data operations. The most common operation is retrieving the data from the database, and those queries start with the SELECT keyword. In other words, a SELECT is asking the database to retrieve some information. Let’s examine the building blocks of this command. Basic SQL SELECT Syntax Databases store information in tables, which organize data into columns and rows. For the example queries in this article, we will use a simple table called employee: idnamesalary 1John Doe1500 2Martin Smith1750 3Alex Bale999 The employee table has three columns: id - A unique identifier for each employee. name - The name of the employee. salary - The salary of the employee. If you want a quick summary of the most common SQL commands and their syntax, check out our free SQL Basics Cheat Sheet. Get All Data from the Table Let’s start with the most basic query - retrieving all data. Here is the query that fetches all rows from the employee table. Note that the asterisk (*) is a shortcut for saying “all the data in all the columns”: SELECT * FROM employee; As you can see, an SQL query closely resembles the English language: “Select * (everything) from [the table called] employee”. Understanding this can help you build your queries faster, as the query looks similar to the basic parts of an English sentence. Here is the result: idnamesalary 1John Doe1500 2Martin Smith1750 3Alex Bale999 As you can see, the result of the query is the same as the initial data. The query has returned all the information in the table. Get Some Columns from the Table Suppose we only want to see employees’ names and their salaries. In this case, we don’t need the database to return all the data in all the columns; we only need to see the data in two columns. We can modify the first query to show only certain columns: SELECT name, salary FROM employee; Here, the * (everything) symbol is replaced with a list of column names. You can specify columns you want to select by just listing their names, separated by commas. Result: namesalary John Doe1500 Martin Smith1750 Alex Bale999 Now the result only includes the columns specified in the query; the id column is not shown. Get Specific Rows from the Table Most times, you will not need all the data in a table. You can also specify which rows you want to get from the database. There is a special clause that allows you to specify how you want to filter the rows: WHERE. Here is an example query that only returns the row with an id of 1: SELECT * FROM employee WHERE id = 1; This query reads: “Select all columns from the table employee where ID is equal to 1”. Result: idnamesalary 1John Doe1500 As you can see, this query selects all the information in the table for the employee with an id of 1. You can use the WHERE clause to compare numbers, text, dates, and much more. For a deeper dive into the WHERE clause, take a look at our article on What is the SQL WHERE Clause. Sort the Result You can use the ORDER BY keyword to specify a way to sort the result. The sorting will be applied at the end, after all other statements are evaluated. Just as with the other queries we’ve seen, this starts with listing the columns that you want the result. Remember to separate the columns with commas. After the column list (and any other clauses you may be using, like WHERE), you place the ORDER BY clause. This will sort the result data by the values in one or more columns. Simply place the column name(s) you want to order the data by after the ORDER BY keyword. You can optionally specify your preferred sort order using the keyword ASC (for ascending order, i.e. 1 to 10, A to Z) or DESC (for descending order, i.e. 10 to 1 or Z to A). Here is an example that shows all employees ordered by their salary, with the highest-paid employees coming first: Result: idnamesalary 2Martin Smith1750 1John Doe1500 3Alex Bale999 The returned rows are sorted by the salary column, with the highest salaries coming first. You could also sort the result by any other column. For example, sorting by name will order the results by employee name, in alphabetical order. The ORDER BY clause always comes last in the query, as it just sorts the final result without changing it. For a closer look at the ORDER BY clause, check out our detailed guide. Want to Learn More SQL Query Syntax? We have covered the basics of SQL syntax – the SELECT, FROM, WHERE, and ORDER BY keywords. Nicely done! Ready to tackle a deeper dive into the basics of SQL? Check out our extensive SQL Basics course, with more than 100 interactive exercises that will help you grasp the syntax of SQL in no time! And if you are looking for different media to learn SQL from, read our detailed guide on the Best Ways to Learn SQL. It compiles different resources so you can pick what’s right for you. Happy learning! Tags: SQL Basics