Select Statement:
It is one of the most basic statements in SQL as it allows us to retrieve information from a table.
Like: SELECT column_name FROM table_name
So, how does this code works:
As excel has multiple pages, SQL has multiple tables in a database. Thus, after knowing which database we are working on we need to retrieve the relevant tables and columns.
Example: We have the following database
Now, If we want 7,8,9 . Then, we will write:
SELECT C3 FROM Table 3
7 |
8 |
9 |
Result
Now if we want to select multiple columns than we specify all the column by using comma “,”.
For 4,5,6 and 7,8,9 from Table 3 we will write the code:
CODE: SELECT C2,C3 FROM Table 3
4 | 7 |
5 | 8 |
6 | 9 |
Result
If we want all the columns of the table then we use asterik “*”
For 1,2,3 and 4,5,6 and 7,8,9 from Table 3 we will use:
Code: SELECT * FROM Table 3
1 | 4 | 7 |
2 | 5 | 8 |
3 | 6 | 9 |
Result
Generally, we do not use * unless we want the whole table as it increases traffic and can slow down your results.
Try this yourself on SQL dvdrental database:
Retrieve the First name and last name from table actor
Retrieve all columns of table actor
Credits: The concepts and materials are taken from the Udemy course:The complete SQL Bootcamp 2022: Go from Zero to Hero
Check out the other parts in the SQL series:
Previous: Why do we need Structured Query Language (SQL)? SQL PART 1