SQL Part 1: What is it?

by Algirdas Grajauskas

SQL is a type of language which is used to manage databases. What does that mean though?

Essentially the written language interacts with a database in order to manipulate the database in ways that are suited for the users for example take, place, delete information from the database.

Therefore, to be able to work with SQL one needs to know the basic lines(words) for it to structure the queries.

* - is a wildcard, which pulls all columns, usually used in combination with the following 6 statements.

  1. SELECT - Selecting the columns you want to take
Example 1

Selects all the columns from superstore database.

2. FROM - Choosing the database that you want to take from

As shown previously in example 1, FROM takes the chosen database where to pull the information from

3. WHERE - Where the column name matches something (Example 3)

Chooses specific column and compares whether it has it. "Column_Name" = "something"

Example 3

4. GROUP BY - Summarizes information for each column name (Example 4)

Example 4

Group by takes the information from select and groups it by the chosen column name, for example here it is grouped by "State". Although be careful, if you have multiple column names that are not aggregated or counted as you can see in the example. The SQL database query will show an error that will say it has a compilation error, this is due to the fact that it wants you to group by every non-aggregated measure. (example 5)

Example 5

5. HAVING - Is only used with aggregated (summarized) information, if it doesn't have an aggregate then use WHERE

Example 6

Having allows to compare information which has been aggregated before. (Example 6) In this example the total value of quantity has been counted, and it was compared to have higher quantity than 50 through HAVING. This would not work with WHERE, as it only applies to string values.

6. ORDER BY - Orders information based on the column can choose ASC|DESC

Example 7 

Order by is a simple one, you just choose a field you want to order by and then choose whether you want to have it Ascending (ASC) or descending (DESC).

As a closing statement I would also like to mention that SQL queries can have operators such as AND, OR, NOT.

AND operator makes sure that the SQL query matches both requests.

OR checks for the query to match one of the two requests

NOT makes sure it doesn't have the specified field.

Hopefully this helps, thank you for reading.

Avatar

Algirdas Grajauskas

Fri 29 Jul 2022

Thu 28 Jul 2022

Wed 27 Jul 2022