The SELECT
statement is the primary means of querying data from a database in SQL. It allows you to specify which columns of data you want to retrieve, from which tables, and under what conditions. Here's a breakdown of some of the components and operators used in SELECT
queries:
1. SELECT
and FROM
SELECT
: Specifies the columns to be retrieved from the database.FROM
: Specifies the table from which to retrieve the data.
Basic Syntax:
sqlSELECT column1, column2, ...FROM table_name;
Example:
sqlSELECT first_name, last_nameFROM students;
This query retrieves the first_name
and last_name
columns from the students
table.
2. WHERE
with Relational Operators
The WHERE
clause is used to filter records based on specific conditions. Relational operators are commonly used in the WHERE
clause to define these conditions.
Relational Operators:
=
: Equals!=
or<>
: Not equal>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal to
Syntax:
sqlSELECT column1, column2, ...FROM table_nameWHERE condition;
Example:
sqlSELECT first_name, last_nameFROM studentsWHERE grade_level >= 10;
This query retrieves the first_name
and last_name
of students who are in grade 10 or higher.
3. BETWEEN
The BETWEEN
operator is used to filter the result set within a certain range. It is inclusive, meaning the values at both ends of the range are included.
Syntax:
sqlSELECT column1, column2, ...FROM table_nameWHERE column_name BETWEEN value1 AND value2;
Example:
sqlSELECT first_name, last_nameFROM studentsWHERE grade_level BETWEEN 9 AND 12;
This query retrieves the first_name
and last_name
of students who are in grades 9 through 12.
4. Logical Operators: AND
, OR
, NOT
Logical operators are used to combine multiple conditions in the WHERE
clause.
AND
: Combines multiple conditions; all conditions must be true.OR
: Combines multiple conditions; at least one condition must be true.NOT
: Reverses the result of a condition.
Syntax:
sqlSELECT column1, column2, ...FROM table_nameWHERE condition1 AND condition2;
Example:
sqlSELECT first_name, last_nameFROM studentsWHERE grade_level >= 10 AND last_name = 'Smith';
This query retrieves the first_name
and last_name
of students who are in grade 10 or higher and have the last name 'Smith'.
Using OR
:
sqlSELECT first_name, last_nameFROM studentsWHERE grade_level = 10 OR grade_level = 12;
This query retrieves the first_name
and last_name
of students who are in either grade 10 or grade 12.
Using NOT
:
sqlSELECT first_name, last_nameFROM studentsWHERE NOT grade_level = 9;
This query retrieves the first_name
and last_name
of students who are not in grade 9.
5. IS NULL
and IS NOT NULL
The IS NULL
and IS NOT NULL
operators are used to filter records with or without null (missing) values.
Syntax:
sqlSELECT column1, column2, ...FROM table_nameWHERE column_name IS NULL;
Example of IS NULL
:
sqlSELECT first_name, last_nameFROM studentsWHERE email IS NULL;
This query retrieves the first_name
and last_name
of students who do not have an email address (i.e., their email
column is NULL
).
Example of IS NOT NULL
:
sqlSELECT first_name, last_nameFROM studentsWHERE email IS NOT NULL;
This query retrieves the first_name
and last_name
of students who have an email address (i.e., their email
column is not NULL
).
0 Comments
Please do note create link post in comment section