ADVANCED
SQL
Chrisandra S. Gaston
Jeremy P. Palada
Argel J. Ramos
Logical, Arithmetic,
Comparison and Bit Operators
OPERATORS are the building blocks of complex queries.
Logical operators allow you to relate numbers of
conditions in various ways.
Arithmetic operators allow you to perform basic
mathematical operations in your queries.
Comparison operators allow you to compare values, and
narrow result sets in this way. Finally, bit operators, while
not often used, allow you to work at a bit level in your
queries.
LOGICAL OPERATORS
Operator
AND, &&
Syntax
Descriptio
c1 AND c2, c1 && c2 Only true if both
conditions c1
and c2 are true
OR, ||
!, NOT
c1 OR c2, c1 || c2
! c1, NOT c1
True if either c1
or c2 is true.
True if c1 is false,
false if c1 is tru
LOGICAL OPERATORS
ARITHMETIC OPERATORS
Operator
+
Syntax
Description
a+b
Adds a and b together, SUM
a-b
Subtracts b from a,
a*b
Multiplies a and b, PRODUCT
a/b
Divides a by b, QUOTIENT
DIFFERENCE
a%b
a modulus b, REMAINDER
ARITHMETIC OPERATORS
Operator
=
!=
>
<
COMPARISON
OPERATORS
Name
Description
Equals
True if both a & b are equ
Not equal
True if a is not equa
Greater than True if a is greater than
Less than True if a is less than b
Name
IS NULL
COMPARISON
OPERATORS
IS NOT NULL
value
BETWEEN
Description
True if a contains a NULL value
True if a doesnt contain NULL
True if a is between the value
of b &c, inclusive
NOT BETWEEN True if a is not between the value
of b &c, inclusive
LIKE
True if a matches b
COMPARISON
OPERATORS
Name
NOT LIKE
IN
NOT IN
anything
RLIKE
exp.
NOT RLIKE
Description
True if a does not match b
True if a is equal to anything
in the list
True if a is not equal to
in the list
True if a matches b in regular
True if a does not match b with
regular expression
EQUALS
NOT EQUALS
Greater than and Less
Than
Greater than
Less than
Greater than and Equal
to
Less than and Equal to
<=>
IS NULL
IS NOT NULL
BETWEEN
NOT BETWEEN
LIKE
NOT LIKE
IN
NOT IN
RLIKE
NOT RLIKE
USING LIKE with
SQL PATTERN MATCHES
-These allow you to compare
against a character youre no t
sure bout instead of specific ones
you know.
Character
%
characters
-
Description
Any number of
One character
BIT OPERATORS
Operator
Syntax
Description
&
a&b
|
a|b
Bitwise AND
Bitwise OR
<<
by b
a << b
Left shift of a
>>
a by b
a >> b
Right shift of
ADVANCED JOINS
INNER JOINS
*These queries were
identical
LEFT & RIGHT JOINS
(LEFT & RIGHT OUTER
JOINS)
FULL OUTER JOINS
-Joins where each record from the first
table, including those with no match in
the
second table, is returned along with each
record in the second table, including
those
with no match in the first.
Syntax to be used:
SELECT field1, field2 FROM table1 FULL OUTER
JOIN table2
NATURAL JOINS &
USING KEYWORD
*rename sales.customer to
sales.id
NOTE:
A NATURAL JOIN can also be a left or
right join. If there is more than one
identical field in the two tables, USING
keyword allows you to specify which of these
fields are used as join conditions.
It also allows more flexibility because it
allows you to use only some of the four
identical fields.
JOINING RESULTS WITH
UNION
Sub-select
Rewriting Sub-selects as Joins
JOINING RESULTS WITH
UNION
REMOVING RECORDS
(Delete and Truncate)
DELETE
informs you how many rows have been removed
TRUNCATE
removes the lot without counting them.
REMOVING RECORDS
(Delete and Truncate)
Performing transactions
with BEGIN &COMMIT
For InnoDB
tables
TRANSACTION
involves wrapping
the
queries together
BEGIN to indicate the start of the
transaction
COMMIT to indicate the end
ROLLBACK to reverse the incomplete
part of the transaction
Performing transactions
with BEGIN &COMMIT
Performing transactions
with BEGIN &COMMIT
Consistent Reads
Consistent Reads
Read Locks for Updating
Read Locks for Sharing
AUTOMATIC COMMITS
*Screenshots goes here*
For BDB tables
TABLE LOCKINGS
Two kinds of table locks:
READ LOCKS
only reads may be performed on the
table,
writes are locked
WRITE LOCKS
no reads and or writes may be performed
on the table for the duration of the lock.
SYNTAX TO BE USED TO LOCK A TABLE:
LOCK TABLE tablename {READ | WRITE}
SYNTAX TO BE USED TO LOCK A TABLE:
UNLOCK TABLES;
WARNING!!!
The LOCK TABLES statement
is not transaction safe. It will
commit all active transactions
before it attempts to lock the
tables.
TRANSACTION LEVELS
READ UNCOMMITED This level
allows transactions to read the
uncommitted data from other
transactions (dirty read)
READ COMMITTED This level
does not allow dirty reads.
REPEATABLE READ This
level does not allow nonrepeatable reads
SERIALIZE- This level does
not allow phantom reads
For changing your
transaction level, use
this syntax:
SET [scope] TRANSACTION
ISOLATION LEVEL
{isolation_level}
END OF
PRESENTATION