SQL Fundamentals: SELECT and WHERE Clauses

This lesson introduces you to the fundamental SQL commands used to retrieve data from a database. You'll learn how to use the `SELECT` and `WHERE` clauses to specify which data you want to see and how to filter that data based on certain conditions. By the end, you'll be able to write basic SQL queries to extract information from a database.

Learning Objectives

  • Understand the purpose and function of the `SELECT` statement.
  • Explain the role of the `FROM` clause in specifying the table to query.
  • Master the use of the `WHERE` clause for filtering data.
  • Write simple SQL queries using `SELECT`, `FROM`, and `WHERE` to retrieve specific data.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to SQL and Retrieving Data

SQL (Structured Query Language) is the standard language for communicating with databases. One of the most common tasks in database administration is retrieving data. This lesson will focus on the foundational commands for retrieving information: the SELECT statement, the FROM clause, and the WHERE clause. These three components work together to help you extract the precise information you need.

The `SELECT` Statement: Choosing What to See

The SELECT statement is used to specify which columns you want to retrieve from a table. You write SELECT followed by the names of the columns you want, separated by commas. If you want to retrieve all columns, you can use the asterisk (*) which is a shortcut for 'all columns'.

Example:
Assume you have a table named Employees with columns like EmployeeID, FirstName, LastName, Salary, and Department.

  • SELECT FirstName, LastName FROM Employees; This query will return only the FirstName and LastName columns for all employees.
  • SELECT * FROM Employees; This will return all columns and all rows in the Employees table.

The `FROM` Clause: Specifying the Table

The FROM clause tells the database where to retrieve the data from – i.e., which table. You always include the FROM clause after the SELECT clause, specifying the table's name.

Example:
* SELECT FirstName, LastName FROM Employees; The FROM Employees part specifies that the data should be retrieved from the Employees table.

The `WHERE` Clause: Filtering Your Data

The WHERE clause is used to filter the data and retrieve only the rows that meet a specific condition. You write WHERE followed by a condition. This condition is a logical expression that can include comparison operators like =, != (not equal), >, <, >=, and <=. You can also use logical operators like AND, OR, and NOT to combine multiple conditions.

Example:
* SELECT FirstName, LastName FROM Employees WHERE Department = 'Sales'; This will return the first and last names of only those employees whose department is 'Sales'.
* SELECT * FROM Employees WHERE Salary > 60000; This will return all columns for all employees whose salary is greater than 60000.
* SELECT EmployeeID, FirstName, LastName FROM Employees WHERE Department = 'Marketing' AND Salary < 50000; This query retrieves only the employee ID, first name, and last name of employees in the Marketing department who earn less than 50000.

Progress
0%