The Basic Commands You Need to Know to Get Started with SQL
Tags: Machine Learning, Artificial Intelligence, Sql, Technology, Data Science

The Basic Commands You Need to Know to Get Started with SQL Moeedlodhi Follow Sep 18 · 5 min read

Getting the fundamentals right is a MUST to mastering SQL.

Photo by Caspar Camille Rubin on Unsplash

SQL is one of the most commonly used tools that you will encounter on your Data Science journey. Before actually working on real-life projects, I had very little respect for SQL’s abilities(out of ignorance) and did not focus much on it but when I entered the real world of “Data Science”, I realized how important SQL is for extracting, transforming and loading data from databases particularly LARGE datasets with a ton of tables.

In this article, I will be going over some basic yet VERY important SQL commands which everyone should be aware of to get a strong start.

I will be writing my queries on Dbeaver, which is a Universal database tool available for download here but you can use any database tool of your choice as long as the concept is clear. Let us get started

1) SELECT and FROM

I am assuming you guys have an SQL database tool ( Dbeaver, pgAdmin) open in front of you.

The first two commands I would like to go over are SELECT and FROM.

SELECT, as the name suggests, selects all columns or column(s) of our choice FROM the table we are working on. For example

I have three columns visible here such as AlbumId, Title, and ArtistId.

If I go to my query editor and type:

SELECT AlbumId,Title

FROM Album

Where Album is the table, I will get only the AlbumId and Title back. Let us see

You can try this with any column and table of your choice. Now, you guys might be thinking, What if I want to return all of my columns? Well, it’s simple,

Just type:

SELECT *

FROM Album

Here, * denotes all of the columns

2) LIMIT

The Limit command as the name suggests “limits” the output of the table i.e We can limit the number of rows we would like to see.

Continuing from our previous example, let us say, I would like to see only the first four rows. Using Limit, We can do that.

SELECT AlbumId,Title

FROM Album

LIMIT 4

and I get:

Moving forward.

3) ORDER BY

ORDER BY rearranges the entire column of our choice both alphabetically and numerically if the values are numerical. This restructures the entire table. Let us give it a try

SELECT *

FROM Album

ORDER BY Title

LIMIT 9

The result:

As you might have noticed, The Title now appears alphabetically in order. Starting from A. And the AlbumId and ArtistId appear to have adjusted accordingly.

4)WHERE

The WHERE command is used to apply conditional statements to our table. For example, If I wanted to return only those rows which had an ArtistId of 90, I would use the WHERE command. Let us give it a try.

SELECT *

FROM Album

WHERE ArtistId = 90

ORDER BY Title

LIMIT 9

Pretty simple no? There are other applications of WHERE as well, such as selecting only those values with specific characters in them or only those where the starting alphabet is A or B. Lets take a look

5) IN and LIKE

The IN and LIKE operators are extremely effective when trying to select specific rows. For example, If I wanted to return only those rows where the Title started with the Alphabet A, How would I do that? Let us see

SELECT *

FROM Album

WHERE Title LIKE 'A%'

i get:

As you can see, All of my Titles start with the alphabet’s A. The ‘A%’ character in my LIKE statement denotes that the Title should start with A. There are other cases to this as well, such as:

‘%A’ all rows where Titles end with A

‘%A%’ All rows that have A anywhere in the Title name

I would recommend you guys practice on the above statements to get a good grip.

The IN command works similarly to the LIKE command but it takes a more general approach. What do I mean by that? For example, I wanted to return all the rows where the Title of the band is Facelift. Now, I could do that by using the LIKE command but there is a better way of doing this.

SELECT *

FROM Album

WHERE Title IN ('Facelift')

Running the above query gives me:

I can use the IN command to apply multiple conditions and return multiple values. For example:

SELECT *

FROM Album

WHERE Title IN ('Facelift','Big Ones','Audioslave')

6) AND & OR

AND and OR are widely used when you are trying to apply multiple conditions and are extremely handy. AND will return values when BOTH the conditions you’re applying are met whereas OR will return all rows for either one of the conditions being met. For example:

Let us say, I would like to return rows where the Title is Audioslave and the AlbumId is 5. Let us give it a try.

SELECT *

FROM Album

WHERE Title IN ('Audioslave') AND AlbumId = 5

The above query returns no rows and rightfully so because there is a row with the Title being Audioslave AND AlbumId being 5.

Let us give the OR command a try.

SELECT *

FROM Album

WHERE Title IN ('Audioslave') OR AlbumId = 5

The OR command does return rows because any of the conditions can be met to generate the results.

Conclusion

So this was a basic introduction to getting started with SQL queries on Dbeaver(or any other database). I went over some very BASIC yet extremely IMPORTANT concepts that are a Must know to get started with SQL. Hopefully, you guys will find this article useful.

If you do, feel free to share it with your peers.

[1]: Moeed Lodhi.. Introduction to SQL on Dbeaver

https://www.youtube.com/watch?v=TGb7ImXsAfo&feature=youtu.be