Basic SQL Queries – Overview For Beginners With Examples

What I have here today is a article for beginners. It will be full of sql query examples. We will start from the most basic ones and continue up. SQL queries will be organized chronologically according to their difficulty. I will add more later on.

SQL knowledge is a must for any IT department nowadays. But these days not only IT guys but also analytics and people working with reports use SQL querying since it is making their work much more effective.

Examples of how can SQL query help you with your work:

1 Simplest SQL Query Without Conditions

Query with all columns selection using * without limiting conditions (tutorial on select clause here):

FROM [AdventureWorksDW2014].[dbo].[udv_SalesByProducts];

simplest sql query for begginers

2 SQL Queries With WHERE Condition

Query with limiting condition on year 2010 in where (tutorial on where clause here):

FROM [AdventureWorksDW2014].[dbo].[udv_SalesByProducts]
WHERE [Year] = 2010;

sql query for begginers with simple where condition

3 Compound Condition in WHERE

SQL query in which we used many conditions and some basic operators IN, BETWEEN, LIKE and >

FROM [AdventureWorksDW2014].[dbo].[udv_SalesByProducts]
WHERE [Product Subcategory] LIKE ('%bike%')
AND [Year] IN (2013,2014)
AND [AverageAmount] > 0;

sql query for begginers with compound where condition

4 Selection of First 10 Records Organized in Ascending Order (ASC) Or In Descending Order (DESC) Using ORDER BY

We select 10 (TOP) highest sales in the year 2013 organized in descending order (ORDER BY <Column> DESC)

FROM [AdventureWorksDW2014].[dbo].[udv_SalesByProducts]
WHERE [Year] = 2013

sql query for begginers order by clause

5 Aggregating Records Using Functions and Clause GROUP BY

We apply aggregation functions  SUM, COUNT, AVG, MAX, MIN on sales through calendar year. Similar sql queries with use of at least 1 aggregation function are usual:

  SUM([Amount])  AS [Amount],
  COUNT(*)       AS [Sales Count],
  AVG([Amount])  AS [Average Amount],
  MAX([Amount])  AS [Max Amount],
  MIN([Amount])  AS [Min_Amount]
FROM [AdventureWorksDW2014].[dbo].[udv_SalesByProducts]

sql query for begginers - aggregate functions

6 Basic Application of HAVING Clause – Condition On Aggregated Data

SQL query on how limit result on records fulfilling the condition after aggregation by using having (we will use base of query as in example 5)

  AVG([Amount]) AS [Average]
FROM [AdventureWorksDW2014].[dbo].[udv_SalesByProducts]
HAVING AVG([Amount]) < 5000

sql query for begginers - using having clause

Rate this post
Category: SQL Basics SQL Clauses Useful SQL Scripts

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, banking 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.