Last week, we had the honor to learn from the one and only 2022 Iron Viz champion, Will Sutton. Today, I will be reflecting on some of the SQL things we learned. I will be mostly focusing on the syntax of a SQL query and potential outputs by queries.
The normal syntax goes in this order;
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY ;
SELECT is used for selecting columns in a table to show when the query runs.
FROM is used to select the table to draw from. You can use the full path in a database for more accuracy, but if the table name is unique, you just need the table name.
WHERE is used for filtering the columns you selected. Some of the simple WHERE clauses include =, <, >, !=, <=, or >=. You can use string values or integer values for filtering. Another way to filter string values is to use a LIKE clause after the WHERE clause.
GROUP BY clauses are used to summarize (or aggregate) the data by one or more column names. Usually includes an aggregation field in the SELECT clause (ie, SUM, AVG).
HAVING is a filter used after the GROUP BY - essentially a "WHERE" clause.
ORDER BY is a sorting tool at the end of the query. ORDER BY clauses can be sorted using DESC or ASC after the desired column name.
End the query with a semicolon to start a new query in the same worksheet.
For example:
SELECT "column_1", "column_2"
FROM "table_1"
WHERE "column_1" = 'string' AND "column_2" > integer
GROUP BY "column_1"
HAVING "column_1" LIKE '%string%'
ORDER BY "column_2" DESC;