SQL is a universal language, used to query data bases. In this blog, you will learn how t0 use SQL in order to get whatever information you require. There are 6 main operations you need to learn.
- SELECT
The SELECT operator is one of the most important operators in the SQL language. It allows you to request what columns you want to return, in order to understand how to use it, you first need to understand the FROM operator.
2. FROM
The FROM operator is the most important operator. Without it, you have no data sources to pull information from. It tends to be written as
SELECT *
FROM "Database"."Schema"."Table"
You can see the select operator here, with this operator, you can either decide to return every column using the *, or you can instruct it to only bring back the columns you want:
SELECT "Column 1"
,"Column 2"
FROM "Database"."Schema"."Table".
For the FROM operator, you must instruct it, the path you want it to follow and not just the table name, or the instruction will error.
3. WHERE
The WHERE operator allows you to 'filter' the data you want to see. Even if the column has not been selected, you are still able to filter using it. You are able to filter by strings:
SELECT *
FROM "Database"."Schema"."Table"
WHERE "Column 1"='string'
or numeric values:
WHERE "Column 2">2000
you can also give it ranges/lists to filter by:
WHERE "Column 3" IN ('cat1','cat2')
or
WHERE "Column 4" BETWEEN 0 and 10
4. Group By
Aggregate the numeric columns, giving a single number for each unique string value in a column. For example: if you wanted to find the total number of sales in Texas, you would have:
SELECT "State"
,sum("Sales")
FROM "Database"."Schema"."Table"
GROUP BY "State"
It is a requirement to have an aggregated numeric value in the select operator and then tell SQL what column you want to group that aggregated value by.
5. HAVING
The HAVING operator must come after a group by if it is to be used. It filters on the newly created aggregated value. For example you wanted to find the states that made a profit.
SELECT "State"
,sum("Profit")
FROM "Database"."Schema"."Table"
GROUP BY "State"
HAVING sum("Profit")>0
6. ORDER BY
And for the final basic SQL operator...ORDER BY. This operator allows you to sort your columns, in either ascending or descending order.
SELECT *
FROM "Database"."Schema"."Table"
ORDER BY "State" ASC
or
ORDER BY "State" DESC
You can also input multiple columns, the order they are inputted, will be the order they are sorted by. e.g
ORDER BY "State" ASC
,"City" ASC
will order by state in ascending order first, then by city in ascending order.