Basic SQL

Today, we'll dive into the basics of retrieving data from relational databases using SQL. You'll learn the fundamental SELECT statement, filtering data with WHERE clauses, and sorting results. This knowledge will form the foundation for more complex data retrieval techniques.

Learning Objectives

  • Understand the purpose and syntax of the SELECT statement.
  • Learn how to use the WHERE clause to filter data based on specific conditions.
  • Be able to use the ORDER BY clause to sort data in ascending and descending order.
  • Grasp the concept of data types and how they affect filtering.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to SELECT

The SELECT statement is the cornerstone of SQL. It's used to retrieve data from one or more tables in a database. The basic syntax is:

SELECT column1, column2, ... FROM table_name;
  • SELECT keyword is used to specify which columns you want to retrieve. Use * to select all columns.
  • FROM keyword indicates the table from which you want to retrieve the data.
  • column1, column2, ... are the names of the columns you want to retrieve. Separate multiple columns with commas.

Example:
Suppose we have a table called Customers with columns CustomerID, FirstName, LastName, and City. To select the FirstName and LastName for all customers, you'd use:

SELECT FirstName, LastName FROM Customers;

To retrieve all columns, you can use:

SELECT * FROM Customers;

Filtering Data with WHERE Clause

The WHERE clause is used to filter the data based on a specified condition. It allows you to retrieve only the rows that meet your criteria. The syntax is:

SELECT column1, column2, ... FROM table_name WHERE condition;

Example:
To select all customers from the city 'New York', you'd use:

SELECT * FROM Customers WHERE City = 'New York';

Important Operators for WHERE Clause:

  • = (equal to)
  • != or <> (not equal to)
  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)
  • AND (both conditions must be true)
  • OR (either condition must be true)
  • BETWEEN (within a range)
  • LIKE (pattern matching)
  • IN (specify multiple possible values)

Examples using different operators:

  • Customers older than 30: SELECT * FROM Customers WHERE Age > 30;
  • Customers in New York or Los Angeles: SELECT * FROM Customers WHERE City = 'New York' OR City = 'Los Angeles';
  • Customers with a name that starts with 'J': SELECT * FROM Customers WHERE FirstName LIKE 'J%';

Sorting Data with ORDER BY Clause

The ORDER BY clause is used to sort the result set. You can sort by one or more columns in ascending (ASC) or descending (DESC) order. The default is ascending (ASC).

SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|DESC;

Example:
To sort customers by LastName in ascending order:

SELECT * FROM Customers ORDER BY LastName ASC;

To sort customers by LastName in descending order and then by FirstName in ascending order:

SELECT * FROM Customers ORDER BY LastName DESC, FirstName ASC;

Data Types and Filtering Considerations

Different data types require different comparison methods.

  • Numbers: You can directly compare numbers using operators like =, >, <, etc. Example: WHERE Age > 25
  • Text/Strings: Text values must be enclosed in single quotes. Example: WHERE City = 'London'. Case sensitivity can depend on your database system.
  • Dates: Date formats vary by database. Typically, dates are enclosed in single quotes. Example: WHERE OrderDate > '2023-01-01'

Understanding data types is crucial for writing accurate WHERE clauses.

Progress
0%