Let's Learn SQL - Part 1
Before we jump into the realm of Sequels, let’s understand why do we need them. For that we need to understand Databases. In simple terms, it is an organized collection of data, usually a tremendous amount of data, stored in a system. The most important aspects of databases are the ability to handle huge amounts of data, facilitating quick and easy combination of different datasets, ability to automate steps for re-use, and provides with data integrity. Consider a database as a collection of tables of data. Each table having its own number of rows and columns.
- SQL (Structured query language) is a programming language we use to communicate with our database: To put data into it, or to get data out of it.
- SQL can be used in a lot of applications or softwares like PostGreSQL, MySQL, Amazon’s Redshift, Looker, MemSQL, Hive (for Hadoop), etc.
Now we will go through the different statements, keywords or functions we need to learn SQL!
The SELECT
statement
One of the most commonly used statements in SQL, is used to retrieve information from a table in a database.
- To select a particular column from a particular table in the database do as follows:
SELECT column_name FROM table_name;
- To select multiple columns from a particular table in the database do as follows:
SELECT column_name1, column_name2 FROM table_name;
- To select all the columns from a particular table in the database do as follows:
SELECT * FROM table_name;
Key Notes to remember:
- Now usually in real applications, you don’t use asterisk as it queries everything in that table and this is inefficient as it might increase the traffic between your database server and the application, which will slow down the retrieving process.
- The Keywords
SELECT
andFROM
are written in capitals as a standard format and for it to be more readable. It’s however, not necessary to use all caps. - The semicolon in the end of every statement means the end of that query statement. It is again a convention you can follow or ignore.
The DISTINCT
Keyword
This keyword is used to query only distinct values from a column. For example, if a column from the table has duplicate values or entries, and you are only interested in querying unique values.
SELECT DISTINCT column_name FROM table_name;
- To view distinct values in multiple columns do as follows:
SELECT DISTINCT column_name1, column_name2 FROM table_name;
The COUNT
function
This function gives us the number of rows in a column or table. Since COUNT
is a function, you must pass the column_name
inside the parenthesis.
SELECT COUNT(column_name) FROM table_name;
To get the number of rows for all the columns at once, do as follows:
SELECT COUNT(*) FROM table_name;
To get the number of rows for only the distinct values in a column, do as follows:
SELECT COUNT(DISTINCT column_name) FROM table_name;
The WHERE
clause
In a SELECT statement, the WHERE clause comes after the FROM clause. It is useful when you want to put conditions on which rows you want returned.
- To select only those rows in a column where the column_name corresponds to
ash
, do as follows:SELECT column_name FROM table_name WHERE column_name = 'ash';
- To select only those rows in multiple columns where the column_name corresponds to
groot
, do as follows:SELECT column_name1, column_name2 FROM table_name WHERE column_name2 = 'groot';
- To select only those rows in multiple columns where the column_name1 corresponds to
groot
, and column_name2 corresponds torocket
do as follows:SELECT column_name1, column_name2 FROM table_name WHERE column_name1 = 'groot' AND column_name2 = 'rocket';
- To select all columns where a column_name either corresponds to
ash
or it corresponds tomaya
SELECT * FROM table_name WHERE column_name = 'ash' OR column_name = 'maya';
Now you might get the gist of it. There are comparison operators are well for you to make conditions. Some of the examples are as follows:
- Suppose you have a table with 3 columns house_name, number_of_people and number_of_dogs in those houses. You want to get only those houses that has just 1 dog.
SELECT house_name FROM table_name WHERE number_of_dogs = 1;
- To get the houses with more than or equal to 2 dogs. More the merrier, isn’t it?
SELECT house_name FROM table_name WHERE number_of_dogs >= 2;
- To get the houses with 2 people and less than 4 dogs:
SELECT house_name FROM table_name WHERE number_of_people = 2 AND number_of_dogs <= 4;
Operator | Meaning |
---|---|
<= | Less than or equal to |
>= | Greater than or equal to |
< | Less than |
> | Greater than |
!= | Not equal to |
= | Equal to |
The ORDER BY
Use this f you want the returns from a query in an orders way (ascending or descending).
SELECT column_name1 FROM table_name ORDER BY column_name2 ASC;
This will order the rows in the column_name1 based on the entries in the column_name2 in ascending order. ASC
is for ascending and is default if you don’t mention it. DESC
is for descending order.
- Coming back to our house analogy, to sort the rows of house_name based on the entries of number_of_people first, and then based on number_of_dogs, do as follows:
SELECT house_name FROM table_name ORDER BY number_of_people, number_of_dogs;
Here the order of columns_name
number_of_people
andnumber_of_dogs
matters. If you writenumber_of_dogs
first, then the rows will be ordered by the dogs and then the people!
Follow the upcoming blog for more into SQL.