This lesson on BASIC SELECT is hands-on and example-driven. You will be able to retrieve specific columns or all data from a single database table using the fundamental SQL SELECT statement. You will learn the basic syntax for specifying columns and the use of the asterisk wildcard character.
What You'll Be Able To Do
- Identify the purpose of the SELECT statement in SQL.
- Write a query to retrieve specific, named columns from a table.
- Construct a query to return all columns from a specified table.
- Apply the correct syntax for listing columns and specifying the source table.
- Differentiate between selecting named columns and using the asterisk wildcard.
Topics Covered in BASIC SELECT
- SELECT statement introduction (0:00 - 0:15) — The SELECT statement is introduced as the primary tool for picking data out of a database.
- Specific column example (0:15 - 0:30) — A simple query is presented to grab the CustomerName and City columns from the Customers table.
- General syntax defined (0:30 - 0:45) — The general structure of the SELECT column list FROM table_name syntax is defined.
- Executing the demo query (0:45 - 1:00) — The example query selecting CustomerName and City is executed against the sample Customers table.
- Wildcard selection (1:00 - 1:15) — The asterisk wildcard is introduced as a way to select all columns in the table.
- Lesson summary (1:15 - 1:30) — The basic functionality of the SQL SELECT statement is summarized as easy to use.
SQL Cheat Sheet
-
SELECT— Specifies which columns or expressions should be returned in the result setSELECT CustomerName, City -
FROM <table>— Indicates the source table containing the data to be retrievedFROM Customers; -
SELECT col1, col2 FROM table;— Retrieves only the specified columns from the designated tableSELECT CustomerName, City FROM Customers; -
*— Wildcard representing all columns in the specified tableSELECT * FROM Customers;
Comparison Table
| Method | Syntax Example | Columns Returned |
|---|---|---|
| Specific Selection | SELECT CustomerName, City | Only CustomerName and City |
| Wildcard Selection | SELECT * | All columns in the table |
| General Form | column one, column two, etc. | Only the explicitly listed columns |
Common Pitfalls
- Mistake: Running a SELECT statement without specifying the source table. Avoid: Always include the FROM table_name clause after listing columns.
- Mistake: Putting the table name in the column list or vice versa. Avoid: List columns after SELECT and the table after FROM.
- Mistake: Using SELECT * when only two columns are needed. Avoid: Specify only the required column names for clarity and efficiency.
- Mistake: Forgetting to separate multiple column names with a comma. Avoid: Use a comma (,) between every column name in the list.
FAQs
- What is the purpose of the semicolon at the end of the query? The semicolon (;) is a statement terminator. It signals to the database that the current SQL command is complete and ready to be executed.
- Do I need to capitalize SELECT and FROM? SQL keywords are generally case-insensitive, meaning the query will run either way. However, capitalization is standard practice for readability.
- Can I select columns from multiple tables at once? Yes, but retrieving data from multiple tables requires using JOINs. This basic lesson focuses only on selecting data from a single table.