SQL Fundamentals

This lesson introduces the fundamentals of SQL (Structured Query Language), the standard language for interacting with databases. You will learn the core commands `SELECT`, `FROM`, and `WHERE` and how to use them to retrieve specific data from a single table.

Learning Objectives

  • Define what SQL is and its purpose in database management.
  • Explain the functionality of the `SELECT`, `FROM`, and `WHERE` clauses.
  • Construct basic SQL queries to retrieve data based on specific criteria.
  • Utilize comparison operators in the `WHERE` clause to filter results.

Text-to-Speech

Listen to the lesson content

Lesson Content

What is SQL?

SQL (Structured Query Language) is a powerful language used to communicate with databases. It allows you to create, read, update, and delete data (CRUD operations) stored in a database. Think of SQL as the translator between you and the database, enabling you to ask questions and get answers in a structured way. This lesson focuses on retrieving information (reading) from a database.

The `SELECT` Statement

The SELECT statement is used to retrieve data from a database. It's how you ask the database for information. You specify which columns you want to see. The general syntax is: SELECT column1, column2, ... FROM table_name;

  • SELECT *: This is a special case of SELECT to select all columns.
  • Example: Imagine a table called Customers with columns like CustomerID, FirstName, LastName, and City. To see all the FirstName and LastName from the Customers table, you'd write: SELECT FirstName, LastName FROM Customers;

The `FROM` Clause

The FROM clause specifies the table from which you want to retrieve data. It tells SQL where to look for the information you're requesting.

  • Example: In the previous example, FROM Customers tells SQL to get the data from the Customers table.

The `WHERE` Clause

The WHERE clause is used to filter the data you retrieve, based on specific conditions. This is how you narrow down your results to only the data you need. You provide a condition that the data must meet.

  • Comparison Operators: You use operators in the WHERE clause to create conditions.
    • = (equal to)
    • != or <> (not equal to)
    • > (greater than)
    • < (less than)
    • >= (greater than or equal to)
    • <= (less than or equal to)
  • Example: To find all customers from the city 'London', you'd write: SELECT FirstName, LastName FROM Customers WHERE City = 'London'; The WHERE City = 'London' filters the results.

Putting it All Together

Let's combine all three elements. Suppose you want to find the CustomerID and FirstName of all customers who live in 'New York' and have a CustomerID greater than 100. Assume the table Customers has a column CustomerID. The SQL query would be: SELECT CustomerID, FirstName FROM Customers WHERE City = 'New York' AND CustomerID > 100; Notice the use of AND operator to combine two conditions. This is more advanced. We will discuss it more in later lessons.

Progress
0%