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.
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.
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.
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'.
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.
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.
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.