Below is a list of things that are great to know about SQL:
The semicolon ( ; ) is used in SQL to end a query. They're not always necessary. You can still execute a SQL query without having a semicolon at the end. However, it's a good habit to create (end every query with a semicolon).
Also, there are times when you'll want to run more than one query at a time. This is referred to as batch processing (i.e. run one query, then the next, then another, ...). In order to run multiple queries in sequence, you'd end each with a semicolon.
Two hyphens, before a line of SQL, will turn that line into a comment. Comments are typically used for documentation purposes, and will not run as a SQL query (even if highlighted) – they're ignored by the database software.
-- For example, this is a comment
/* Multi-Line Comments */
Another way to add comments to a SQL query, especially if it's a multi-line comment, is to use the forward slash and asterisk. All text within the two parameters will be compiled as a comment.
/* For example, this is one line of a multi-line comment And this is a second line */
SQL is Space Insensitive
Spaces don't really matter in SQL. They are necessary for separating one piece of logic from another, but additional space doesn't make a difference:
-- For example, this: SELECT * FROM getting_started.users ; -- Is the same as: SELECT * FROM getting_started.users ; -- And the same as: SELECT * FROM getting_started.users ; -- But this is incorrect, and will result in an error: SELECT*FROMgetting_started.users;
SQL Style Guide(s)
There's no right way to style your SQL. There are only preferred guidelines to help you, and others, keep your SQL organized. Do some research on various styles, pick one you like and stick with it.
There are layers to the internal architecture of database software: Database, Schema, and Table. So, when we reference a table in a SQL query, you'll want to reference those layers to properly tell the database where you want to pull data from:
/* Most times, you'll select a database in a SQL Client. So, it's common to just reference the schema and table name. For example, the four tables in the SFDS Getting Started schema are: */ getting_started.users getting_started.payments -- As an example: SELECT * FROM getting_started.users; /* However, you can reference all three layers as well. If you titled your database "sfdataschool", here's how you'd reference the four tables in the SFDS Getting Started schema: */ sfdataschool.getting_started.users sfdataschool.getting_started.payments -- As an example: SELECT * FROM sfdataschool.getting_started.users;
"Highlight-to-Run" in a SQL Client
Highlighting the SQL you want to run is one of the most common ways to execute a query in a SQL Client. For example, in the below image, even though there are two queries outside of the one highlighted, only the highlighted query will be executed when hitting "Run":
SQL Pulls, Not Manipulates Data
SQL pulls data from a database, versus manipulating it at the source. For contrast, when you filter and sort data in a spreadsheet, you're legitimately manipulating the data. With SQL, data gets pulled and manipulated as an abstraction. As in, the actual data in the database isn't being changed.
There are ways to change data in a database with SQL, but it's most common (as an Analytics professional) to query and manipulate data at an abstraction layer.
Is it Pronounced "Sequel" or "S-Q-L"
We prefer "Sequel", but it doesn't matter. So long as others know what you're referring to.