Basics to SQL

Structured Query Language (SQL) is a fundamental skill for data professionals as it enables them to access and manipulate databases. If you're already familiar with data preparation tools like Tableau Prep or Alteryx, learning SQL becomes even more intuitive due to its similar principles and syntax. You might wonder if it's necessary to learn SQL if you're already good at using tools like Alteryx and Tableau Prep. However, not all companies have these tools. So, knowing SQL is really important for working with data in many different places. Plus, lots of Data Analysts use SQL, showing how important it is in the field.

In this blog post, we'll discuss on the basics of some essential SQL keywords: SELECT, FROM, WHERE, GROUP BY, HAVING, and ORDER BY. We will reference to the following 'Orders' table to demonstrate the queries.

Orders

SELECT

The SELECT statement in SQL enables you to specify which columns you want to extract from a table. In the following query, we are retrieving the Customer, Product, and Price columns from the Orders table.

Result:

FROM

The FROM clause is an integral part of the SELECT statement and serves to specify the source of the data you intend to retrieve. As exemplified above, we utilize the FROM clause to extract information from the Orders table.

WHERE

The WHERE clause in SQL filter records based on specific conditions. It enables you to selectively retrieve data from a table based on defined criteria. In the example query below, we are extracting information from the orders table where the product is 'ramen'.

Result:

GROUP BY

The GROUP BY clause in SQL is used to group rows with the same values in a specified column or columns into summary rows. It is typically used in conjunction with aggregate functions like COUNT, SUM, AVG, MAX, and MIN to perform operations on each group of rows. In the subsequent query, we are aggregating the total price for each customer by grouping them together.

Result:

HAVING

The HAVING clause in SQL is used in conjunction with the GROUP BY clause to filter the results of a grouped query. It allows you to apply a condition to the aggregated data and include or exclude groups based on that condition. In the query below, we are retrieving customers who have spent a total price exceeding 30 after the GROUP BY summarization.

Results:

ORDER BY

The ORDER BY clause in SQL is used to sort the result set of a query in either ascending (ASC) or descending (DESC) order based on one or more columns. In the query below, we retrieve the Customer, Product, and Price columns from the Orders table, while also sorting the resulting table by ascending Customer and Order_Date.

Result:

Author:
Nitesh Shrestha
Powered by The Information Lab
1st Floor, 25 Watling Street, London, EC4M 9BR
Subscribe
to our Newsletter
Get the lastest news about The Data School and application tips
Subscribe now
© 2024 The Information Lab