No history yet

Introduction to SQL

The Language of Databases

SQL, or Structured Query Language, is the special language we use to communicate with relational databases. Think of a database as a massive, highly organized digital filing cabinet. SQL is the language you use to ask that filing cabinet to find, add, or change specific files.

Every time you use an app that stores information—like your contacts, your social media posts, or your online shopping history—there's a good chance a database is working behind the scenes, and SQL is the language making it all happen.

The Building Blocks

Relational databases organize data in a simple, intuitive way: using tables. If you've ever used a spreadsheet, you're already familiar with the basic idea.

  • Tables: A table is a collection of related data. For example, you might have a table for Customers, another for Products, and a third for Orders.
  • Columns: Each table is made up of columns, which describe a specific piece of information. In a Customers table, you'd have columns like customer_id, first_name, and email.
  • Rows: A row represents a single record in the table. Each row in the Customers table would be one specific customer, with their own ID, name, and email.
customer_idfirst_namelast_nameemail
1MariaAndersmaria.anders@example.com
2AnaTrujilloana.trujillo@example.com
3AntonioMorenoantonio.m@example.com

In this Customers table, each horizontal line is a row, and each vertical section like first_name is a column. All the data together makes up the table.

Asking for Data

The most common task in SQL is retrieving data. We do this using a SELECT statement. This statement tells the database exactly what information you want to see.

A SELECT statement has two main parts to start: SELECT and FROM.

  • SELECT specifies the columns you want.
  • FROM specifies the table you want them from.

If you want to see everything in the Customers table, you can use an asterisk (*) which is a shortcut for "all columns".

SELECT * FROM Customers;

This query asks the database to show us every column from every row in the Customers table. But what if you only need the first names and emails? You just list those columns specifically.

SELECT first_name, email
FROM Customers;

Notice the semicolon (;) at the end of each statement. This tells the database that your command is complete. While not always required, it's a good habit to always include it.

Filtering Your Results

Getting all your data is useful, but often you only want rows that meet certain criteria. That's where the WHERE clause comes in. It lets you filter your data based on a condition.

Let's say we only want to see the customer with the customer_id of 2.

SELECT first_name, last_name
FROM Customers
WHERE customer_id = 2;

This query would return a single row: Ana Trujillo. The WHERE clause filtered out all other customers.

You can use other operators besides the equals sign (=). For example, if we had an age column, we could find all customers older than 30.

SELECT first_name, age
FROM Customers
WHERE age > 30;

You can also use WHERE with text. When you're looking for a specific text value, you need to wrap it in single quotes.

SELECT *
FROM Customers
WHERE first_name = 'Antonio';

With SELECT, FROM, and WHERE, you have the core tools to retrieve very specific data from any table. It's the foundation upon which all other SQL operations are built.