Appearance
Basic Queries
To query data from a database table, we use the following SQL statement:
sql
SELECT * FROM <table_name>
Assuming the table name is students
, to query all rows from the students
table, we use the following SQL statement:
sql
-- Query all data from the students table
SELECT * FROM students;
When using SELECT * FROM students
, SELECT
is a keyword indicating a query, *
represents "all columns," and FROM
indicates which table to query, in this case, the students
table. This SQL will retrieve all data from the students
table, and the result will also be a two-dimensional table containing column names and each row of data.
To query all rows from the classes
table, we use:
sql
-- Query all data from the classes table
SELECT * FROM classes;
Running the above SQL statement will show the query results.
The SELECT
statement does not always require a FROM
clause. For example:
sql
-- Calculate 100 + 200
SELECT 100 + 200;
This query will directly compute the result of the expression. Although SELECT
can be used for calculations, it is not its primary strength. However, a SELECT
statement without a FROM
clause is useful for checking if the current database connection is valid. Many monitoring tools execute SELECT 1;
to test the database connection.
Summary
The basic SELECT
query SELECT * FROM <table_name>
can retrieve all rows and all columns of data from a table. The result of a SELECT
query is a two-dimensional table.