RienSQL
LESSON 10 / 62 · Basic SQL Query Syntax

Basic 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_idstatusmember_namebirthday
1fatherHeadley Quincey1960-05-13T00:00:00.000Z
2motherFlavia Quincey1963-02-16T00:00:00.000Z
3sonAndie Quincey1983-06-05T00:00:00.000Z
4daughterLela Quincey1985-06-07T00:00:00.000Z
5daughterAnnie Quincey1988-04-10T00:00:00.000Z
6fatherErnest Forrest1961-09-11T00:00:00.000Z
7motherConstance Forrest1968-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_idmember_name
1Headley Quincey
2Flavia Quincey
3Andie Quincey
4Lela Quincey
5Annie Quincey
6Ernest Forrest
7Constance 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_idName
1Headley Quincey
2Flavia Quincey
3Andie Quincey
4Lela Quincey
5Annie Quincey
6Ernest Forrest
7Constance 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 FamilyMembers

Aliases 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.

Task 1Print string
Using SELECT operator to print the text "Hello world" without giving the column an alias.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 2SELECTing all columns
Select all columns from Payments table.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 3SELECTing by multiple columns
Pick member_id, member_name and status fields from FamilyMembers table.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 4Print with aliases
Print the name field from the Passenger table. When displaying this field, use the alias "passengerName".
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.