7.7 KiB
7.7 KiB
title:: SQL DML: SELECT
- #CT230 - Database Systems I
- Previous Topic: SQL DML Statement
- Next Topic: SQL SELECT: Working with Strings & Subqueries
- Relevant Slides:
-
Notes on Syntax
- While SQL is case insensitive, Linux ^^is not.^^
- web1.cs.nuigalway.ie is a ^^linux server^^, so we need to be careful with table names in particular, as
EMPLOYEE
is ^^not the same thing^^ asemployee
.
- web1.cs.nuigalway.ie is a ^^linux server^^, so we need to be careful with table names in particular, as
- Attribute names are separated by commas.
- Strings are enclosed in quotes.
- Numbers are not enclosed in quotes.
- Usually, SQL keywords are capitalised while table & keyword names are mostly kept in lowercase unless using camelCase instead of snake_case.
- Code should be organised horizontally & vertically, and not all written on one line.
- Code blocks are separated by a semicolon.
- Comments can be made using
#
,--
, &/* */
.
- While SQL is case insensitive, Linux ^^is not.^^
-
CRUD Operations in DML
- What are CRUD Operations? #card
card-last-interval:: 7.44
card-repeats:: 3
card-ease-factor:: 2.08
card-next-schedule:: 2022-11-22T02:35:29.285Z
card-last-reviewed:: 2022-11-14T16:35:29.286Z
card-last-score:: 3
- CRUD operations are the 4 basic functions that we may wish to perform on persistent data.
- Create: insert a new tuple.
INSERT
- Read: retrieve some data.
SELECT
- Update: modify some data.
Update
- Delete: delete some data or a tuple.
DELETE
- Create: insert a new tuple.
- CRUD operations are the 4 basic functions that we may wish to perform on persistent data.
-
Read:
SELECT
- The basic syntax for an SQL select query to read data consists of 6 clauses. #card
card-last-interval:: 19.01
card-repeats:: 4
card-ease-factor:: 2.18
card-next-schedule:: 2022-12-03T20:06:00.414Z
card-last-reviewed:: 2022-11-14T20:06:00.414Z
card-last-score:: 3
-
SELECT [DISTINCT] <attribute list> FROM <table list> WHERE <condition> GROUP BY <group attributes> HAVING <group condition> ORDER BY <attribute list>
- The order of these clauses ^^cannot be changed.^^
SELECT
&FROM
are ^^always required^^, other clauses are optional.
-
-
card-last-interval:: 4 card-repeats:: 2 card-ease-factor:: 2.9 card-next-schedule:: 2022-11-26T13:37:49.433Z card-last-reviewed:: 2022-11-22T13:37:49.433Z card-last-score:: 5SELECT <attribute list> FROM <table list> WHERE <condition>
#card<attribute list>
is a list of attribute (column) names (separated by commas) whose values will be retrieved by the query.<table list>
is a list of table names (separated by commas) containing the attributes.<condition>
is a Boolean expression that identifies the tuples to be retrieved by the query.- For each tuple (row) in the table(s) which are part of the query:
- the tuple is checked to see if the condition is true for this tuple.
- If true, the tuple is part of the output.
- If false, the tuple is not part of the output.
- the tuple is checked to see if the condition is true for this tuple.
- The comparison operators are:
=
<=
<
>
>=
!=
- Conditions can be compounded by use of Boolean
AND
,OR
, and can be negated withNOT
. - ^^Note:^^ In some versions of SQL, (e.g., in MS), the
!=
operator is written as<>
.
- For each tuple (row) in the table(s) which are part of the query:
-
Calculated or Derived Fields #card
card-last-interval:: 28.3 card-repeats:: 4 card-ease-factor:: 2.66 card-next-schedule:: 2022-12-15T16:48:00.918Z card-last-reviewed:: 2022-11-17T09:48:00.918Z card-last-score:: 5- We can specify an SQL expression in the
SELECT
clause which can involve numerical operations on numeric fields and counting operations on non-numeric fields. - e.g.,
-
SELECT ssn, salary/12 FROM employee;
-
- We can specify an SQL expression in the
-
Tidying Up the Output #card
card-last-interval:: 19.01 card-repeats:: 4 card-ease-factor:: 2.18 card-next-schedule:: 2022-12-03T20:05:40.995Z card-last-reviewed:: 2022-11-14T20:05:40.995Z card-last-score:: 3- We can use keywords
CAST
,AS
, &DECIMAL(x, y)
to specify the total number of digitsx
and the number of digits after the decimal pointy
when we're working with real numbers.-
SELECT ssn, CAST(salary/12.0 AS DECIMAL(8,2)) FROM employee;
-
- We can also use the keyword
AS
to rename output.-
SELECT ssn, CAST(salary/12.0 AS DECIMAL(8,2)) AS monthlySalary FROM employee;
-
- We can use keywords
-
Keyword
card-last-interval:: 33.64 card-repeats:: 4 card-ease-factor:: 2.9 card-next-schedule:: 2022-12-21T00:50:05.938Z card-last-reviewed:: 2022-11-17T09:50:05.938Z card-last-score:: 5DISTINCT
#card- The keyword
DISTINCT
automatically removes duplicates from the returned result set. - We should be careful of using with large result sets as it can be an expensive operation to perform (not a problem for our small examples).
-
SELECT DISTINCT salary FROM employee;
- The keyword
-
Selecting All Attribute Values for Selected Tuples #card
card-last-interval:: 11.2 card-repeats:: 3 card-ease-factor:: 2.8 card-next-schedule:: 2022-11-25T20:36:38.405Z card-last-reviewed:: 2022-11-14T16:36:38.405Z card-last-score:: 5- To retrieve all attribute values of selected tuples, you do not have to explicitly list all the attribute names - instead, you can use
SELECT *
.
- To retrieve all attribute values of selected tuples, you do not have to explicitly list all the attribute names - instead, you can use
-
New Operators
BETWEEN
#card card-last-interval:: 33.64 card-repeats:: 4 card-ease-factor:: 2.9 card-next-schedule:: 2022-12-18T11:07:12.625Z card-last-reviewed:: 2022-11-14T20:07:12.625Z card-last-score:: 5- The
BETWEEN
operator selects values within a given range. The values can be numbers, texts, or dates. -
SELECT <column name(s)> FROM <table name> WHERE <column name(s)> BETWEEN <value 1> AND <value 2>;
- The
IN
#card card-last-interval:: 24.2 card-repeats:: 4 card-ease-factor:: 2.42 card-next-schedule:: 2022-12-11T13:48:17.496Z card-last-reviewed:: 2022-11-17T09:48:17.496Z card-last-score:: 3- The
IN
operator tests if a data value matches one of a list of values. - It is a shorthand for multiple
OR
conditions. -
SELECT <column name(s)> FROM <table_name> WHERE <column_name> IN (<value1>, <value2>, ...); -- OR -- SELECT <column_name(s)> FROM <table_name> WHERE <column_name> IN (SELECT <statement>);
- The
LIKE
#card card-last-interval:: 33.64 card-repeats:: 4 card-ease-factor:: 2.9 card-next-schedule:: 2022-12-18T11:07:20.197Z card-last-reviewed:: 2022-11-14T20:07:20.198Z card-last-score:: 5- Allows string comparison, when equality is too strict.
- There are 2 wildcards, often used in conjunction with the
LIKE
operator:- The percent sign
%
represents zero, one, or multiple characters. - The underscore sign
_
represents one, single character.
- The percent sign
-
SELECT <column_name(s)> FROM <table_name> WHERE <column_name> LIKE <pattern>;
IS NULL
#card card-last-interval:: 33.64 card-repeats:: 4 card-ease-factor:: 2.9 card-next-schedule:: 2022-12-21T00:49:58.730Z card-last-reviewed:: 2022-11-17T09:49:58.730Z card-last-score:: 5- Allows an explicit search for
NULL
.
- Allows an explicit search for
-
Set Operators
UNION
INTERSECTION
MINUS / DIFFERENCE
- The basic syntax for an SQL select query to read data consists of 6 clauses. #card
card-last-interval:: 19.01
card-repeats:: 4
card-ease-factor:: 2.18
card-next-schedule:: 2022-12-03T20:06:00.414Z
card-last-reviewed:: 2022-11-14T20:06:00.414Z
card-last-score:: 3
- What are CRUD Operations? #card
card-last-interval:: 7.44
card-repeats:: 3
card-ease-factor:: 2.08
card-next-schedule:: 2022-11-22T02:35:29.285Z
card-last-reviewed:: 2022-11-14T16:35:29.286Z
card-last-score:: 3
-
SELECT full_name, ssn FROM ( SELECT ssn, CONCAT(fname, " ", minit, " ", lname) AS full_name from employee) AS dtable WHERE full_name IN ("John B Smith", "James E Borg", "Joyce A English");