• 15. 12. 2019
  • Ing. Jan Zedníček - Data Engineer & Controlling
  • 0

This mini-article is made for beginners. We will take a closer look on how to write a simple T-SQL script and I will also explain basic sql commands. We will put together individual parts of script piece by piece and we will understand its purpose during the process. Let’s dig into SQL basics in following example.

SQL Basics – SQL query assembly step by step

Task: Imagine table containing data on people. Task is to pick everyone in table “People” who was born between years 1995 and 2005 including. In the result of the script, we are interested in name, surname and date of birth. All records should be organized in descending order by surname.

SELECT Command

This clause states that we want do display some data. We also define in it which data (Columns) to pick:

Continuous SQL script:

SELECT
  Name,
  Surname,
  DateOfBirth

If we would execute this script above a database it would result in error. Reason for this is that we did not declare from where fields “Name”, “Surname” and “DateOfBirth” get selected. It is needed to select the table.

Command FROM

Clause defines where are information from previous step located = in which table will we find them. We will search in table “People”

Continuous SQL script:

SELECT
  Name,
  Surname,
  DateOfBirth
FROM People

This script will finally be functional and it will return all records from table “People”. Selected columns will be Name, Surname and DateOfBirth.

Command WHERE

It is time to restrict the result from previous step to DateOfBirth. This clause generally serves to define restricting conditions.

Continuous SQL script:

SELECT
  Name,
  Surname,
  DateOfBirth
FROM People
WHERE DateOfBirth>= '1995-01-01' AND DateOfBirth<= '2005-12-31'

Condition consists of column we need to limit somehow. We can see that we can use mathematical operators as ><= etc. in SQL. Condition can be simplified by using operator BETWEEN.

In this case the condition would look like this: WHERE DateOfBirth BETWEEN '1995-01-01' AND '2005-12-31'

Command ORDER BY

Last part of the task is to sort the result. We do that through command ORDER BY where we can add next to it how we want the data to be organized. (ASC – ascending order or  DESC – descending order) (not necessary). If we do not fill in the sorting requirement, data will be sorted in ascending order.

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 to understand basic SQL command and SQL basics. I recommend to go through articles on this web if you prefer self-studying.

You can start by going through articles concerning SQL clauses:

3/5 - (1 vote)

Ing. Jan Zedníček - Data Engineer & Controlling

My name is Jan Zedníček and I have been working as a freelancer for many companies for more than 10 years. I used to work as a financial controller, analyst and manager at many different companies in field of banking and manufacturing. When I am not at work, I like playing volleyball, chess, doing a workout in the gym.

🔥 If you found this article helpful, please share it or mention me on your website

Leave a Reply

Your email address will not be published. Required fields are marked *