This mini-article is intended for beginners. We will take a closer look at how to write a simple T-SQL script, and I will also explain basic SQL commands. We will assemble the individual parts of the script step by step and explain their purpose as we proceed. Let’s dive into SQL basics in the following example.
SQL Basics – SQL query assembly step by step
Task: Imagine a table containing data about people. The task is to select everyone from the table “People” who was born between the years 1995 and 2005 (inclusive). In the result set, we are interested in the name, surname, and date of birth. All records should be sorted in descending order by surname.
SELECT Command
This clause specifies that we want to display certain data. It also defines which data (columns) should be selected:
Continuous SQL script:
SELECT Name, Surname, DateOfBirth
If we were to execute this script against a database, it would result in an error. The reason is that we did not specify the source of the fields “Name”, “Surname”, and “DateOfBirth”. We need to define the table from which these fields are selected.
Command FROM
This clause defines where the information from the previous step is located—in other words, which table contains the data. In this case, we will query the table “People”.
Continuous SQL script:
SELECT Name, Surname, DateOfBirth
FROM People
This script is now valid and will return all records from the table “People”. The selected columns will be Name, Surname, and DateOfBirth.
Command WHERE
Next, we need to restrict the result set based on the DateOfBirth column. This clause is generally used to define filtering conditions.
Continuous SQL script:
SELECT Name, Surname, DateOfBirth
FROM People
WHERE DateOfBirth>= '1995-01-01' AND DateOfBirth<= '2005-12-31'
The condition consists of the column that needs to be limited. As you can see, SQL supports mathematical comparison operators such as >, <, and =. This condition can be simplified by using the SQL operator BETWEEN.
In this case, the condition would look like this: WHERE DateOfBirth BETWEEN '1995-01-01' AND '2005-12-31'.
Command ORDER BY
The final part of the task is to sort the result set. This is done using the ORDER BY command, which allows us to define how the data should be organized (ASC for ascending order or DESC for descending order). If no sorting direction is specified, the data is sorted in ascending order by default.
Final script:
SELECT Name, Surname, DateOfBirth
FROM People
WHERE DateOfBirth>= '1995-01-01' AND DateOfBirth<= '2005-12-31'
ORDER BY DateOfBirth DESC
I hope this article helped you understand basic SQL commands and SQL fundamentals. I recommend exploring other articles on this website if you prefer self-study.
You can start by reviewing articles related to SQL clauses, or you can visit the SQL tutorial navigation page:
- SELECT – Selection of Columns From the Table – Basic Clause
- SELECT DISTINCT – Removing Duplicates from Table = Unique values
- WHERE – Adding a condition using SQL
- GROUP BY – Aggregating Data in Table
- ORDER BY – Sorting Data in SQL Table in Descending or Ascending Order
- Difference between WHERE and HAVING – SQL HAVING vs WHERE – What’s the difference?