SQL Select
What is the SELECT statement in SQL?
The SQL SELECT statement is used to query and retrieve data from a database. SELECT is one of the most commonly used SQL statements, allowing you to select specific columns and rows of data from database object(s).
The basic syntax of a SELECT statement is as follows:
SELECT column1, column2, ...
FROM table_name;
- The SELECT clause specifies the column(s) to be retrieved.
- The FROM clause specifies the object(s) from which the data should be retrieved. In this case, column1 and column2 data will be retrieved from table_name.
If you use a “*” in the SELECT statement, you will retrieve data from all columns from the object(s) in the FROM clause. For example, this query returns all the columns from the tv_show table in the SANDBOX schema.
SELECT *
FROM SANDBOX.tv_show;
If you want to specify the columns retrieved, you could write the following statement which will return the SHOW_NAME column and the YEAR_LAUNCH column from the tv_show table.
SELECT
t.SHOW_NAME
, t.YEAR_LAUNCH
FROM
SANDBOX.tv_show t;
In the query above, the table name is given an alias (nickname) t
. Prepending the table name (or alias, in this case) qualifies the column name. This becomes important when querying several tables at once — imagine two tables in one query having a SHOW_NAME
column.
If you are using Coginiti Pro, you can create SELECT statements even faster for your database objects. Hover over an object in the database object tree, right-click and choose “SELECT statement to either the clipboard” (or current tab / new tab). This will write the SELECT statement code for you with all of the details for a given object. You can paste the code into an editor tab, put them in your current editor tab, or open a new tab with the code.
When a SELECT statement is executed, the results are normally returned to a data grid, similar to a spreadsheet, for you to view the data.