Unit-Ii 15
Unit-Ii 15
Unit-Ii 15
SQL SELECT statement is used to fetch the data from a database table which returns data in the form
of result table. These result tables are called result-sets.
Syntax:
The basic syntax of SELECT statement is as follows:
Here, column1, column2...are the fields of a table whose values you want to fetch. If you want to fetch
all the fields available in the field, then you can use the following syntax:
Example:
Consider the CUSTOMERS table having the following records:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Following is an example, which would fetch ID, Name and Salary fields of the customers available in
CUSTOMERS table:
+----+----------+----------+
| ID | NAME | SALARY |
+----+----------+----------+
| 1 | Ramesh | 2000.00 |
| 2 | Khilan | 1500.00 |
| 3 | kaushik | 2000.00 |
| 4 | Chaitali | 6500.00 |
| 5 | Hardik | 8500.00 |
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00 |
+----+----------+----------+
If you want to fetch all the fields of CUSTOMERS table, then use the following query:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
The SQL WHERE clause is used to specify a condition while fetching the data from single table or
joining with multiple tables.
If the given condition is satisfied then only it returns specific value from the table. You would use
WHERE clause to filter the records and fetching only necessary records.
The WHERE clause is not only used in SELECT statement, but it is also used in UPDATE, DELETE
statement, etc., which we would examine in subsequent chapters.
Syntax:
The basic syntax of SELECT statement with WHERE clause is as follows:
You can specify a condition using comparison or logical operators like >, <, =, LIKE, NOT, etc. Below
examples would make this concept clear.
Example:
Consider the CUSTOMERS table having the following records:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Following is an example which would fetch ID, Name and Salary fields from the CUSTOMERS table
where salary is greater than 2000:
+----+----------+----------+
| ID | NAME | SALARY |
+----+----------+----------+
| 4 | Chaitali | 6500.00 |
| 5 | Hardik | 8500.00 |
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00 |
+----+----------+----------+
Following is an example, which would fetch ID, Name and Salary fields from the CUSTOMERS table for
a customer with name Hardik. Here, it is important to note that all the strings should be given inside
single quotes ('') where as numeric values should be given without any quote as in above example:
+----+----------+----------+
| ID | NAME | SALARY |
+----+----------+----------+
| 5 | Hardik | 8500.00 |
+----+----------+----------+
The SQL AND and OR operators are used to combine multiple conditions to narrow data in an SQL
statement. These two operators are called conjunctive operators.
These operators provide a means to make multiple comparisons with different operators in the same
SQL statement.
Syntax:
The basic syntax of AND operator with WHERE clause is as follows:
You can combine N number of conditions using AND operator. For an action to be taken by the SQL
statement, whether it be a transaction or query, all conditions separated by the AND must be TRUE.
Example:
Consider the CUSTOMERS table having the following records:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Following is an example, which would fetch ID, Name and Salary fields from the CUSTOMERS table
where salary is greater than 2000 AND age is less tan 25 years:
+----+-------+----------+
| ID | NAME | SALARY |
+----+-------+----------+
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00 |
+----+-------+----------+
The OR Operator:
The OR operator is used to combine multiple conditions in an SQL statement's WHERE clause.
Syntax:
The basic syntax of OR operator with WHERE clause is as follows:
You can combine N number of conditions using OR operator. For an action to be taken by the SQL
statement, whether it be a transaction or query, only any ONE of the conditions separated by the OR
must be TRUE.
Example:
Consider the CUSTOMERS table having the following records:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Following is an example, which would fetch ID, Name and Salary fields from the CUSTOMERS table
where salary is greater than 2000 OR age is less tan 25 years:
+----+----------+----------+
| ID | NAME | SALARY |
+----+----------+----------+
| 3 | kaushik | 2000.00 |
| 4 | Chaitali | 6500.00 |
| 5 | Hardik | 8500.00 |
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00 |
+----+----------+----------+
JOINS
• JOINs can be used to combine tables
• There are many types of JOIN
• CROSS JOIN
• INNER JOIN
• NATURAL JOIN
• OUTER JOIN
• OUTER JOINs are linked with NULLs - more later
A CROSS JOIN B
• returns all pairs of rows from A and B
A NATURAL JOIN B
• returns pairs of rows with common values for identically named columns and without
duplicating columns
A INNER JOIN B
• returns pairs of rows satisfying a condition
Student
ID Name
123 John
124 Mary
125 Mark
126 Jane
Enrolment
ID Code
123 DBS
124 PRG
124 DBS
126 PRG
ID Name ID Code
123 John 123 DBS
124 Mary 123 DBS
125 Mark 123 DBS
126 Jane 123 DBS
123 John 124 PRG
124 Mary 124 PRG
125 Mark 124 PRG
126 Jane 124 PRG
123 John 124 DBS
124 Mary 124 DBS
NATURAL JOIN
Student
ID Name
123 John
124 Mary
125 Mark
126 Jane
Enrolment
ID Code
123 DBS
124 PRG
124 DBS
126 PRG
ID Name Code
123 John DBS
124 Mary PRG
124 Mary DBS
126 Jane PRG
INNER JOIN
• INNER JOINs specify a condition which the pairs of rows satisfy
SELECT * FROM A INNER JOIN B ON <condition>
Student
ID Name
123 John
124 Mary
125 Mark
126 Jane
Enrolment
ID Code
123 DBS
124 PRG
124 DBS
126 PRG
ID Name ID Code
123 John 123 DBS
124 Mary 124 PRG
124 Mary 124 DBS
126 Jane 126 PRG
Buyer
Name Budget
Smith 100,000
Jones 150,000
Green 80,000
Property
Address Price
15 High St 85,000
12 Queen St 125,000
87 Oak Row 175,000
• Yes, because
• They often lead to concise queries
• NATURAL JOINs are very common
• No, because
• Support for JOINs varies a fair bit among SQL dialects
This Lecture in Exams
Track
cID Num Title Time aID
1 Violent 239 1
2 Every Girl 410 1
3 Breather 217 1
4 Part of Me 279 1
1 Star 362 1
2 Teaboy 417 2
CD
cID Title Price
1 Mix 9.99
2 Compilation 12.99
Artist
aID Name
1 Stellar
2 Cloudboy
Operators are used to specify conditions in an SQL statement and to serve as conjunctions for multiple
conditions in a statement.
Arithmetic operators
Comparison operators
Logical operators
Show Examples
<> Checks if the values of two operands are equal or not, if (a <> b)
values are not equal then condition becomes true. is true.
> Checks if the value of left operand is greater than the value (a > b) is
of right operand, if yes then condition becomes true. not true.
< Checks if the value of left operand is less than the value of (a < b) is
right operand, if yes then condition becomes true. true.
>= Checks if the value of left operand is greater than or equal (a >= b)
to the value of right operand, if yes then condition becomes is not
true. true.
<= Checks if the value of left operand is less than or equal to (a <= b)
the value of right operand, if yes then condition becomes is true.
true.
!< Checks if the value of left operand is not less than the value (a !< b)
of right operand, if yes then condition becomes true. is false.
!> Checks if the value of left operand is not greater than the (a !> b)
value of right operand, if yes then condition becomes true. is true.
Show Examples
Operator Description
ALL The ALL operator is used to compare a value to all values in another
value set.
ANY The ANY operator is used to compare a value to any applicable value in
the list according to the condition.
BETWEEN The BETWEEN operator is used to search for values that are within a
set of values, given the minimum value and the maximum value.
EXISTS The EXISTS operator is used to search for the presence of a row in a
specified table that meets certain criteria.
NOT The NOT operator reverses the meaning of the logical operator with
which it is used. Eg: NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is a
negate operator.
IS NULL The NULL operator is used to compare a value with a NULL value.
UNIQUE The UNIQUE operator searches every row of a specified table for
uniqueness (no duplicates).
SQL FUNCTIONS
SQL aggregate functions return a single value, calculated from values in a column.
SQL scalar functions return a single value, based on the input value.
SQL numeric functions are used primarily for numeric manipulation and/or mathematical calculations.
The following table details the numeric functions:
Name Description
BIT_COUNT() Returns the string representation of the binary value passed to it.
BIT_OR() Returns the bitwise OR of all the bits in the passed expression.
CEIL() Returns the smallest integer value that is not less than passed
numeric expression
CEILING() Returns the smallest integer value that is not less than passed
numeric expression
EXP() Returns the base of the natural logarithm (e) raised to the power of
passed numeric expression.
FLOOR() Returns the largest integer value that is not greater than passed
numeric expression.
OCT() Returns the string representation of the octal value of the passed
numeric expression. Returns NULL if passed value is NULL.
POW() Returns the value of one expression raised to the power of another
expression
POWER() Returns the value of one expression raised to the power of another
expression
SQL string functions are used primarily for string manipulation. The following table details the
important string functions:
Name Description
EXPORT_SET() Returns a string such that for every bit set in the value
bits, you get an on string and for every unset bit, you get
an off string
Set operators are used to join the results of two (or more) SELECT statements.The SET operators
available in Oracle 11g are UNION,UNION ALL,INTERSECT,and MINUS.
The UNION set operator returns the combined results of the two SELECT statements.Essentially,it
removes duplicates from the results i.e. only one row will be listed for each duplicated result.To
counter this behavior,use the UNION ALL set operator which retains the duplicates in the final
result.INTERSECT lists only records that are common to both the SELECT queries; the MINUS set
operator removes the second query's results from the output if they are also found in the first query's
results. INTERSECT and MINUS set operations produce unduplicated results.
All the SET operators share the same degree of precedence among them.Instead,during query
execution, Oracle starts evaluation from left to right or from top to bottom.If explicitly parentheses are
used, then the order may differ as parentheses would be given priority over dangling operators.
Points to remember -
Same number of columns must be selected by all participating SELECT statements.Column
names used in the display are taken from the first query.
Data types of the column list must be compatible/implicitly convertible by oracle. Oracle will
not perform implicit type conversion if corresponding columns in the component queries
belong to different data type groups.For example, if a column in the first component query is of
data type DATE, and the corresponding column in the second component query is of data type
CHAR,Oracle will not perform implicit conversion, but raise ORA-01790 error.
Positional ordering must be used to sort the result set. Individual result set ordering is not
allowed with Set operators. ORDER BY can appear once at the end of the query. For example,
UNION and INTERSECT operators are commutative, i.e. the order of queries is not important; it
doesn't change the final result.
Performance wise, UNION ALL shows better performance as compared to UNION because
resources are not wasted in filtering duplicates and sorting the result set.
Set operators can't be used in SELECT statements containing TABLE collection expressions.
The LONG, BLOB, CLOB, BFILE, VARRAY,or nested table are not permitted for use in Set
operators.For update clause is not allowed with the set operators.
UNION
When multiple SELECT queries are joined using UNION operator, Oracle displays the combined result
from all the compounded SELECT queries,after removing all duplicates and in sorted order (ascending
by default), without ignoring the NULL values.
Consider the below five queries joined using UNION operator.The final combined result set contains
value from all the SQLs. Note the duplication removal and sorting of data.
NUM
-------
1
3
5
6
To be noted, the columns selected in the SELECT queries must be of compatible data type. Oracle
throws an error message when the rule is violated.
UNION ALL
UNION and UNION ALL are similar in their functioning with a slight difference. But UNION ALL gives
the result set without removing duplication and sorting the data. For example,in above query UNION
is replaced by UNION ALL to see the effect.
Consider the query demonstrated in UNION section. Note the difference in the output which is
generated without sorting and deduplication.
NUM
-------
1
5
3
6
3
INTERSECT
Using INTERSECT operator, Oracle displays the common rows from both the SELECT statements, with
no duplicates and data arranged in sorted order (ascending by default).
For example,the below SELECT query retrieves the salary which are common in department 10 and
20.As per ISO SQL Standards, INTERSECT is above others in precedence of evaluation of set operators
but this is not still incorporated by Oracle.
SELECT SALARY
FROM employees
WHERE DEPARTMENT_ID = 10
INTRESECT
SELECT SALARY
FROM employees
WHERE DEPARTMENT_ID = 20
SALARY
---------
1500
1200
2000
MINUS
Minus operator displays the rows which are present in the first query but absent in the second query,
with no duplicates and data arranged in ascending order by default.
SELECT JOB_ID
FROM employees
WHERE DEPARTMENT_ID = 10
MINUS
SELECT JOB_ID
FROM employees
WHERE DEPARTMENT_ID = 20;
JOB_ID
-------------
HR
FIN
ADMIN
In the below query, the data type of employee name (varchar2) and location id (number) do not match.
Therefore, execution of the below query would raise error due to compatibility issue.
ERROR at line 1:
ORA-01790: expression must have same datatype as corresponding expression
Explicitly, columns can be matched by substituting NULL for location id and Employee name.
The compund query below unifies the results from two departments and sorts by the SALARY column.
SUBQU SUBQUERIES
Example:
SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_salary "Salary" FROM
employee WHERE emp_salary = (SELECT MIN(emp_salary) FROM employee);
Last Name First Name Salary
SUBQUERY TYPES
• There are three basic types of subqueries. We will study each of these in the remainder of this
chapter.
1. Subqueries that operate on lists by use of the IN operator or with a comparison operator
modified by the ANY or ALL optional keywords. These subqueries can return a group of values,
but the values must be from a single column of a table.
2. Subqueries that use an unmodified comparison operator (=, <, >, <>) – these subqueries must
return only a single, scalar value.
3. Subqueries that use the EXISTS operator to test the existence of data rows satisfying specified
criteria.
A subquery SELECT statement is very similar to the SELECT statement used to begin a regular or
outer query. The complete syntax of a subquery is shown below.
Rules Cont’d
• The SELECT clause of a subquery must contain only one expression, only one aggregate
function, or only one column name.
• The value(s) returned by a subquery must be join-compatible with the WHERE clause of the
outer query.
Example
SELECT emp_last_name "Last Name", emp_first_name "First Name" FROM employee WHERE emp_ssn
IN (SELECT dep_emp_ssn FROM dependent);
------------- ---------------
Bock Douglas
Zhu Waiman
Joyner Suzanne
Rules Cont’d
• In addition to concerns about the domain of values returned from a subquery, the data type of
the returned column value(s) must be join-compatible.
• Join-compatible data types are data types that the Oracle Server will convert automatically
when matching data in criteria conditions.
• The Oracle Server will automatically convert among any of the following ANSI numeric data
types when making comparisons of numeric values because they all map into the Oracle
NUMBER data type.
• int (integer)
• smallint (small integer)
• decimal
• float
• Oracle does not make comparisons based on column names.
• Columns from two tables that are being compared may have different names as long as they
have a shared domain and the same data type or convertible data types.
• The DISTINCT keyword cannot be used in subqueries that include a GROUP BY clause.
• Subqueries cannot manipulate their results internally. This means that a subquery cannot
include the ORDER BY clause, the COMPUTE clause, or the INTO keyword.
• Subqueries that are introduced with the keyword IN take the general form:
– WHERE expression [NOT] IN (subquery)
• The only difference in the use of the IN operator with subqueries is that the list does not
consist of hard-coded values.
Example
SELECT emp_last_name "Last Name", emp_first_name "First Name" FROM employee WHERE emp_ssn
IN (SELECT dep_emp_ssn FROM dependent WHERE dep_gender = 'M');
--------------- ---------------
Bock Douglas
Zhu Waiman
Joyner Suzanne
DEP_EMP_S
---------
999444444
999555555
999111111
• Next, these social security number values are substituted into the outer query as the listing
that is the object of the IN operator. So, from a conceptual perspective, the outer query now
looks like the following.
SELECT emp_last_name "Last Name", emp_first_name "First Name" FROM employee WHERE emp_ssn
IN (999444444, 999555555, 999111111);
--------------- ---------------
Joyner Suzanne
Zhu Waiman
Bock Douglas
• Like the IN operator, the NOT IN operator can take the result of a subquery as the operator
object.
SELECT emp_last_name "Last Name", emp_first_name "First Name" FROM employee WHERE emp_ssn
NOT IN (SELECT dep_emp_ssn FROM dependent);
--------------- ---------------
Bordoloi Bijoy
Markis Marcia
Amin Hyder
• The subquery shown above produces an intermediate result table containing the social
security numbers of employees who have dependents in the dependent table.
• Conceptually, the outer query compares each row of the employee table against the result
table. If the employee social security number is NOT found in the result table produced by the
inner query, then it is included in the final result table.
Example
SELECT emp_last_name "Last Name", emp_first_name "First Name" FROM employee WHERE emp_ssn
IN (SELECT work_emp_ssn FROM assignment WHERE work_hours > 10 AND work_pro_number IN
(SELECT pro_number FROM project WHERE pro_name = 'Order Entry') );
--------------- ---------------
Bock Douglas
Prescott Sherri
Understanding SUBQUERIES
• In order to understand how this query executes, we begin our examination with the lowest
subquery.
• We will execute it independently of the outer queries.
PRO_NUMBER
----------
• Now, let's substitute the project number into the IN operator list for the intermediate subquery
and execute it.
• The intermediate result table lists two employee social security numbers for employees that
worked more than 10 hours on project #1.
SELECT work_emp_ssn FROM assignment WHERE work_hours > 10 AND work_pro_number IN (1);
WORK_EMP_SSN
-----------------------
999111111
999888888
• Finally, we will substitute these two social security numbers into the IN operator listing for the
outer query in place of the subquery.
SELECT emp_last_name "Last Name", emp_first_name "First Name" FROM employee WHERE emp_ssn
IN (999111111, 999888888);
--------------- ---------------
Bock Douglas
Prescott Sherri
• The general form of the WHERE clause with a comparison operator is similar to that used thus
far in the text.
• Note that the subquery is again enclosed by parentheses.
• The most important point to remember when using a subquery with a comparison operator is
that the subquery can only return a single or scalar value.
• This is also termed a scalar subquery because a single column of a single row is returned by the
subquery.
• If a subquery returns more than one value, the Oracle Server will generate the “ ORA-01427:
single-row subquery returns more than one row ” error message, and the query will fail to
execute.
• Let's examine a subquery that will not execute because it violates the "single value" rule.
• The query shown below returns multiple values for the emp_salary column.
-------------------
55000
43000
43000
• If we substitute this query as a subquery in another SELECT statement, then that SELECT
statement will fail.
• This is demonstrated in the next SELECT statement. Here the SQL code will fail because the
subquery uses the greater than (>) comparison operator and the subquery returns multiple
values.
SELECT emp_ssn FROM employee WHERE emp_salary > (SELECT emp_salary FROM employee
WHERE emp_salary > 40000);
ERROR at line 4:
SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_salary "Salary" FROM
employee WHERE emp_salary > (SELECT AVG(emp_salary) FROM employee);
• The ALL and ANY keywords can modify a comparison operator to allow an outer query
to accept multiple values from a subquery.
• The general form of the WHERE clause for this type of query is shown here.
• Subqueries that use these keywords may also include GROUP BY and HAVING clauses.
• The ALL keyword modifies the greater than comparison operator to mean greater than
all values.
SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_salary "Salary" FROM
employee WHERE emp_salary > ALL (SELECT emp_salary FROM employee WHERE emp_dpt_number
= 7);
Example
SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_salary "Salary" FROM
employee WHERE emp_salary > ANY (SELECT emp_salary FROM employee WHERE emp_salary >
30000);
Last Name First Name Salary
SELECT emp_last_name "Last Name", emp_first_name "First Name" FROM employee WHERE emp_ssn
IN (SELECT dep_emp_ssn FROM dependent WHERE dep_gender = 'M');
SELECT emp_last_name "Last Name", emp_first_name "First Name" FROM employee WHERE emp_ssn
= ANY (SELECT dep_emp_ssn FROM dependent WHERE dep_gender = 'M');
OUTPUT
--------------- ---------------
Bock Douglas
Zhu Waiman
Joyner Suzanne
• However, the "!= ANY" comparison operator and keyword combination shown in this next
WHERE clause means "NOT $38,000" OR "NOT $43,000" OR "NOT $55,000".
CORRELATED SUBQUERIES
• A correlated subquery is one where the inner query depends on values provided by the outer
query.
• This means the inner query is executed repeatedly, once for each row that might be selected by
the outer query.
SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_dpt_number "Dept",
emp_salary "Salary" FROM employee e1 WHERE emp_salary = (SELECT MAX(emp_salary) FROM
employee WHERE emp_dpt_number = e1.emp_dpt_number);
Output
• The subquery in this SELECT statement cannot be resolved independently of the main query.
• Notice that the outer query specifies that rows are selected from the employee table with an alias
name of e1.
• The inner query compares the employee department number column (emp_dpt_number) of the
employee table to the same column for the alias table name e1.
• The value of e1.emp_dpt_number is treated like a variable – it changes as the Oracle server
examines each row of the employee table.
• The subquery's results are correlated with each individual row of the main query – thus, the
term correlated subquery.
• The SELECT statement shown below adds the ORDER BY clause to specify sorting by first name
within last name.
• Note that the ORDER BY clause is placed after the WHERE clause, and that this includes the
subquery as part of the WHERE clause.
SELECT emp_last_name "Last Name", emp_first_name "First Name" FROM employee WHERE EXISTS
(SELECT * FROM dependent WHERE emp_ssn = dep_emp_ssn) ORDER BY emp_last_name,
emp_first_name;
Output:
---------- ---------------
Bock Douglas
Joyner Suzanne
Zhu Waiman
LOCK TABLE
Lock:
A lock is a mechanism to control concurrent access to a data item
Lock Manager: Managing locks on data items.
Types of Locks
1. Binary locks
2. Shared/Exclusive locks
Binary Locks
A binary lock can have two states
Locked (1)
Unlocked (0)
Rules to be followed by Transactions
– Transaction must lock data item before read_item or write_item operations
– Transaction must unlock data item after all read(x)and write(x) operation are
completed in T
– A transaction T will not issue a lock_item(x) operation if it already holds the lock
on item(x)
– A transaction T will not issue an unlock_item(x) operation unless it already holds
the lock on item(x)