SQL Basics – The Most Commonly Used Statements for Beginners

Last modified date:

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:

My name is Jan Zedníček and I work as a BI Developer at Kentico Software in Brno. Mostly you can see me there working in the office, but I also work partly as a freelancer. I have been working as a freelancer for many companies for more than 5 years, but Kentico is the matter of my heart. I used to work as a financial controller at companies like Aero Vodochody or Sberbank and I also used to be a bond program manager in Unicapital Investment group. When I am not at work, I like playing volleyball, chess, doing a workout in the gym and I enjoy tasting of best quality rums. I am trying to summarize all my knowledge on this website not to forget them (because of the rum effect, you know =) and to put them forward to anyone. Don´t worry about asking for help or writing some comments.

Category: SQL Basics

About Ing. Jan Zedníček - BI Developer, Finance controller

My name is Jan Zedníček and I work as a BI Developer at Kentico Software in Brno. Mostly you can see me there working in the office, but I also work partly as a freelancer. I have been working as a freelancer for many companies for more than 5 years, but Kentico is the matter of my heart. I used to work as a financial controller at companies like Aero Vodochody or Sberbank and I also used to be a bond program manager in Unicapital Investment group. When I am not at work, I like playing volleyball, chess, doing a workout in the gym and I enjoy tasting of best quality rums. I am trying to summarize all my knowledge on this website not to forget them (because of the rum effect, you know =) and to put them forward to anyone. Don´t worry about asking for help or writing some comments.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.