SQL Basics: SELECT and FROM
1. SELECT Statement
The SELECT
statement is the most fundamental command in SQL. It is used to retrieve data from a database. The data returned is stored in a result table, often referred to as the result-set.
Imagine you have a library of books, and you want to list all the titles. The SELECT
statement is like asking the librarian to give you a list of all the book titles.
Example:
SELECT title FROM books;
In this example, the SELECT
statement retrieves the title
column from the books
table.
2. FROM Clause
The FROM
clause specifies the table from which to retrieve the data. It is always used in conjunction with the SELECT
statement.
Continuing with the library analogy, the FROM
clause is like specifying which shelf or section of the library you want to look at.
Example:
SELECT title FROM books;
Here, the FROM
clause indicates that the data should be retrieved from the books
table.
Combining SELECT and FROM
When you combine the SELECT
statement with the FROM
clause, you can effectively query the database to get the specific information you need.
Example:
SELECT title, author FROM books WHERE genre = 'Science Fiction';
In this example, the query retrieves the title
and author
columns from the books
table where the genre
is 'Science Fiction'.