Basics in SQL

by Charalambos Pavlou

SQL Query is a very commonly used hardcoding language with a completely different syntax from Tableau and Alteryx. I will cover the basics I have picked up in the session during this blog.

Getting started:

The image above shows the layout of SQL in snowflake to start any query you need to start with the function SELECT this function is used to call fields from a table after SELECT you can enter * to include all fields or select certain fields by calling them in a string function ("") then after you have called your columns double tap on the table header you are using to populate the (FROM TIL PLAYGROUND KEEP section). Remember when listing multiple fields put a comma between (,) them.

Once you have selected your chosen fields you can then begin to edit the data for example the WHERE function can be used to filter but for the WHERE function you do not need to call the field in the SELECT part, you can call it after but all sub headers need to be called in single quotes ' ' unlike fields in double quotes " " for example WHERE "Region" = 'Central'. Another thing to be careful about is SQL is case sensitive so you need to be very careful.

Another thing to remember about SQL is all calculations take place in the SELECT section so for example if you want to calculate Sum(Sales) it needs to be in the SELECT section and write AS to give the Sum("Sales") an Alias of  "Total Sales" then as you can see below you can then divide both Alias's to get "Profit Ratio" (which is also an alias ) which again takes place in the same section. Another point to look at below is Count Distinct needs to be written in a certain format COUNT(DISTINCT "Field"). You then have to Group BY all Fields called at the beginning that are not calculations.

The IN function above also allows you to list multiple regions instead of writing and with syntax WHERE "Region" IN('Central', 'West') this is filtering to the central and west regions.

Top allows you to filter to just the first few records for example and goes just after the SELECT e.g TOP 10. Another important thing to consider is ORDER BY which acts as a sorting function DESC for descending and ASC for ascending you can also list by different fields to sort by for example ORDER BY "Segment" DESC, "Total Sales" DESC, this is saying sort descending for segment and then  sum sales.

The HAVING function works like a where function but only works on aggregated measures, for HAVING however it always needs to go after the GROUP BY whereas WHERE goes before the GROUP BY. UPPER and LOWER just put that field into upper or lower case.

The LIKE function is case sensitive whereas the ILIKE function is case insensitive with the ILIKE function Like TOP not being coloured in blue. The example using WHERE SUBSTRING("Product Name",6,3) ILIKE 'wes' this is saying look at the 6th character and keep the ones with wes at the 6th character with length of 3 as in the wes. the next part replaces ville with banana using the replace function in the SELECT section then filtering by WHERE and %ville% this allows the replace function to work with the %% needed to replace only ville.

Avatar

Charalambos Pavlou

Fri 29 Jul 2022

Thu 28 Jul 2022

Wed 27 Jul 2022

Tue 26 Jul 2022