About queries

2023-07-21 09:11:01  Updated

SQL queries are used to obtain data from databases. A query can be used with various clauses, such as conditional clauses (like WHERE) and sort clauses (like ORDER BY), to obtain query results.

Syntax

An SQL query is a SELECT statement used to query data from one or multiple tables or views. The SELECT statement can complete simple single-table queries and complex connection queries and embedded queries.

Syntax for the simplest SQL statement:

SELECT <select_list> [ FROM <table_list>]

Parameters in the function:

  • select_list specifies the columns, or functions, constants, or variables from the table_list that follows.

  • table_list specifies the tables or views that contain the data to be retrieved.

Usage notes

table_list can also specify a subquery statement that contains the WHERE clause to ensure that the returned result complies with certain conditions. Examples:

SELECT * FROM table_a t1 WHERE t1.pk IN (SELECT t2.pk FROM table_b t2);

In MySQL mode, the FROM table_list clause is optional. Without this clause, select_list does not specify a specific column but a constant or variable. Examples:

obclient> SELECT 'Medium', 6*6 ;
+---------+-----+
| Medium  | 6*6 |
+---------+-----+
| Medium  |  36 |
+---------+-----+
1 row in set

Contact Us