Basic Queries in SQL:: SELECT FROM WHERE
Basic Queries in SQL:: SELECT FROM WHERE
WHERE <condition>
Where:
[ WHERE <condition>]
FROM EMPLOYEE
QUERY 1:
Retrieve the name and address of all employees who work for the
‘Research’ department.
QUERY 2:
For every project located in “Stafford”, list the project number, the
controlling department number, and the department manager’s last
name, address, and birthdate.
Q2:
If a query refers to two or more attributes with the same name, we must
qualify the attribute name with the relation name and separate them
with a dot(.). This is done by prefixing the relation name to the attribute
name.
Ambiguity also arises in the case of queries that refer to the same
relation twice as in the following
QUERY:
For each employee, retrieve the employee’s first and last name and the
first and last name of his or her immediate supervisor.
WHERE E.SUPERSSN=S.SSN
An alias can follow the keyword AS or it can directly follow the relation
name as in : FROM EMPLOYEE E, EMPLOYEE S
SELECT SSN
FROM EMPLOYEE;
SELECT *
FROM EMPLOYEE
SELECT *
SELECT ALL and SELECT are equivalent in that they do not eliminate
duplicate tuples.
FROM EMPLOYEE;
FROM EMPLOYEE;
SET Operations:
UNION
);
The first SELECT query retrieves the projects that involve ‘Smith’ as a
manager of the department that controls the project.
The second SELECT query retrieves the projects that involve ‘Smith’ as
worker on the project.
SQL also has corresponding multiset operations which use the set
operator followed by the keyword ALL (UNION ALL, INTERSECT ALL,
EXCEPT ALL). Their results are bags in that duplicates are not
eliminated.
The LIKE operator is used for comparing strings values in tuples and for
string pattern matching.
FROM EMPLOYEE
FROM EMPLOYEE
Note:
-If the single quote symbol ‘ or apostrophe is part of the string, then they are
entered as “ so it will not be interpreted as the string ending symbol.
For date, time and interval data types, operators include incrementing (+)
decrementing (-) a date, a time or timestamp by an interval.
SELECT *
FROM EMPLOYEE
ORDER BY clause allows the user to order the result of the query by the
values of one or more attributes.
The default order is the ascending order of values. We can specify the order
using either DESC for descending or ASC for ascending.
\b Backspace
\n New line
\r Carriage return
\t Tab
\\ Backslash
LOGICAL: The logical operators return the value TRUE or FALSE, they are:
NOT (or !)
AND (or &&)
OR (or ||)
XOR
COMPARISON
= (equal)
>=(greater than or equal)
> (greater than)
<=( less than or equal)
< (less than)
<> ( not equal or !=)
IS ( NULL test)
BETWEEN (value is in a specified range)
The IS operator can be used to test whether a value is NULL or NOT NULL as
in:
STRING:
The string operators LIKE and NOT LIKE are used for string pattern
matching:
PRECEDENCE:
AND
BETWEEN
=, >=, >, <=, <, <>, !=, IS, LIKE, NOT LIKE
-,+
*, /
-(unary minus)
FUNCTIONS:
AVG
SUM
MIN
MAX
SQL in a nutshell:
Comments: Comments start with -- and are terminated with the end of line
or if there are multiple lines, we use the pairs /* .. */.
Names: Database items such as databases , tables, and columns are names
using identifiers. An identifier is a sequence of letters and digits that have a
maximum length of 128 characters and must begin with a letter. Names are
case sensitive.
Datatype Comment