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:

Simplest SQL Query Without Conditions

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

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

simplest sql query for begginers

SQL Queries With WHERE Condition

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

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

sql query for begginers with simple where condition

Compound Condition in WHERE

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

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

sql query for begginers with compound where condition

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)

SELECT TOP 10 *
FROM [AdventureWorksDW2014].[dbo].[udv_SalesByProducts]
WHERE [Year] = 2013
ORDER BY [Amount] DESC;

sql query for begginers order by clause

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:

SELECT
  [Year],
  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]
GROUP BY [Year]
ORDER BY [Year] ASC;

sql query for begginers - aggregate functions

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)

SELECT
  [Year],
  AVG([Amount]) AS [Average]
FROM [AdventureWorksDW2014].[dbo].[udv_SalesByProducts]
GROUP BY [Year]
HAVING AVG([Amount]) < 5000
ORDER BY [Year] ASC;

sql query for begginers - using having clause

Rate this post

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 *