Image for post
Image for post

SQL (Structured Query Language) is the native language for interacting with databases & is designed for exactly this purpose. It is a language of databases. A database models real-life entities like professors & universities by storing them in tables. Each table contains data from a single entity type. This reduces redundancy by storing entities only once. For example, there only needs to be one row of data containing a certain company’s details. Lastly, a database can be used to model the relationship between entities.

Querying Databases

While SQL can be used to create & modify databases, this tutorial’s focus will be on querying databases. A query is a request for data from a database table (or combination of tables). Querying is an essential skill for a data scientist since the data you need for your analyses will often live in databases.

In SQL, we can select data from a table using a SELECT statement. For example, the following query selects the name column from the people table:

Image for post
Image for post

In this query, SELECT & FROM are called keywords. In SQL, keywords are not case-sensitive, which means you can write the same query as:

Image for post
Image for post

That said, it’s good practice to make SQL keywords uppercase to distinguish them from other parts of your query, like column & table names. It’s also good practice to include a semicolon at the end of your query. This tells SQL where the end of the query is.

SQL Order of Execution

Image for post
Image for post

Note: Your query will always need a SELECT & a FROM statement (to identify which columns you want returned from which table) -the others are optional.

Example of SELECT

In the following example, we will SELECT the title column from the films table.

Image for post
Image for post

When we run the above code, it produces the following result:

Image for post
Image for post

RELATED LINKS

Written by

Data Scientist & Machine Learning Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store