LESSON 10 / 62 · Basic SQL Query SyntaxBasic SQL Query Syntax
An example of simple SQL queries to a database. Using aliases in SQL queries.
One of the main functions of SQL is to get data samples from the DBMS. To do this, SQL uses the SELECT operator. Let's look at a few simple queries that use it.
Output of arbitrary values
To begin with, it is important to understand that using the SELECT operator, you can output data not only from database tables, but also arbitrary rows, numbers, dates, etc. For example, this way you can output an arbitrary string:
SELECT 'Hello world'Output of all data from the table
The * symbol is used to output all fields from a specific table. Let's take a look at the database schema and output the data from one of the tables.
member_idINTgood_idINTgood_type_idINTpayment_idINT
SELECT * FROM FamilyMembers| member_id | status | member_name | birthday |
|---|---|---|---|
| 1 | father | Headley Quincey | 1960-05-13T00:00:00.000Z |
| 2 | mother | Flavia Quincey | 1963-02-16T00:00:00.000Z |
| 3 | son | Andie Quincey | 1983-06-05T00:00:00.000Z |
| 4 | daughter | Lela Quincey | 1985-06-07T00:00:00.000Z |
| 5 | daughter | Annie Quincey | 1988-04-10T00:00:00.000Z |
| 6 | father | Ernest Forrest | 1961-09-11T00:00:00.000Z |
| 7 | mother | Constance Forrest | 1968-09-06T00:00:00.000Z |
Output of data from certain columns of the table
If it is necessary to display information only on certain columns of the table, and not all at once, then this can be done by listing the column names separated by commas:
SELECT member_id, member_name FROM FamilyMembers| member_id | member_name |
|---|---|
| 1 | Headley Quincey |
| 2 | Flavia Quincey |
| 3 | Andie Quincey |
| 4 | Lela Quincey |
| 5 | Annie Quincey |
| 6 | Ernest Forrest |
| 7 | Constance Forrest |
Aliases
In case we want to output some columns of the table, but so that they are named differently in the final selection, we can use aliases.
Their syntax is quite simple, we have to use the AS operator. As in the example below:
SELECT member_id, member_name AS Name FROM FamilyMembers| member_id | Name |
|---|---|
| 1 | Headley Quincey |
| 2 | Flavia Quincey |
| 3 | Andie Quincey |
| 4 | Lela Quincey |
| 5 | Annie Quincey |
| 6 | Ernest Forrest |
| 7 | Constance Forrest |
Or you can do without it by simply writing the desired field name separated by a space.
SELECT member_id, member_name Name FROM FamilyMembersAliases can contain up to 255 characters (including spaces, numbers, and special characters)
Practice
Solve it in the editor. The query runs against a sample database in your browser.
Write a query and run it to see the results.
Write a query and run it to see the results.
Write a query and run it to see the results.
Write a query and run it to see the results.