When it comes to queries, it’s important to understand keywords. Keywords are reserved for operations, and the two most common keywords are SELECT and FROM. SELECT indicates what field or column to get the value. FROM is how you indicate what table you’re pulling from.
As an example, our query would look like this:
SELECT name
FROM clients; (a best practice is to end with a semicolon).
If you want multiple items, then it would be as follows:
SELECT cliend_id, name
FROM clients;
If you want to select all of your fields, rather than typing them all out, simply write SELECT * and keep the FROM clients;
If you need to rename columns you can use “aliasing”. The keyword to accomplish this is AS. Let’s say you had a table that has names and years, and to be more clear, you preferred first name. All you’d need to do is type: SELECT name AS first name, (whatever other field you wanted to include in your query). You won’t change the original table data either.
Now, let’s say you’re looking at a dataset that has the year someone started at a company but you’re just looking at the unique value for the year. The DISTINCT keyword allows you to query the data without listing duplicates. This keyword also works if you want to return unique combinations.
After you’ve run some queries, you’re likely going to want to save the new tables. “Views” are virtual tables that are a result of a saved SQL SELECT statement. It saves the query that generated the result, not the specific data itself. This can be beneficial because the results will dynamically update if the original data in the table changes. While it doesn’t save the data you can still run a query from a view:
CREATE VIEW view_name AS
SELECT column1, column2, …
FROM table_name
This just scratches the surface but should help give a frame of reference on SQL basics!
