SQL SELECT
Every SQL query will begin with the SELECT command to fetch data from one or more tables.
Syntax
The SELECT command is followed by one or more column names to be returned from the table(s) listed in the FROM clause:
SELECT column1, column2, ... columnN FROM table;
SELECT can also use the special * character to represent all columns from the table(s):
SELECT * FROM table;
Note: It is best practice to not rely on column order when using *.
Examples
The following example selects all columns from table1 and table2 with an INNER JOIN, restricting the rows to where table1.columnA and table2.columnB are equal:
SELECT * FROM table1 INNER JOIN table2 ON table1.columnA = table2.columnB;
When selecting all columns from across multiple tables, duplicate column names will generate an error unless they are distinguished with an alias.
The next example selects all columns from table1 and a named column, columnA, from table2. It is presumed that both tables have a column named columnA and, therefore, an alias should be used to avoid an error:
SELECT table1.*, table2.columnA AS colA, table2.columnB FROM table1 INNER JOIN table2 ON table1.columnA = table2.columnB;
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn SQL on Codecademy
- Learn to analyze data with SQL and prepare for technical interviews.
- Includes 9 Courses
- With Certificate
- Beginner Friendly.17 hours
- Learn how to query SQL databases and design relational databases to efficiently store large quantities of data.
- Includes 5 Courses
- With Certificate
- Beginner Friendly.13 hours