0% found this document useful (0 votes)
5 views19 pages

Week2 & 3 (Part 1)

The document provides an overview of the SQL query language, detailing its components such as data-definition language (DDL), data-manipulation language (DML), integrity constraints, view definitions, and transaction control. It explains how SQL facilitates querying and modifying data, including operations like natural joins, aggregation, and set comparisons. Additionally, it covers the use of embedded SQL and dynamic SQL within programming languages.

Uploaded by

memsahni2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views19 pages

Week2 & 3 (Part 1)

The document provides an overview of the SQL query language, detailing its components such as data-definition language (DDL), data-manipulation language (DML), integrity constraints, view definitions, and transaction control. It explains how SQL facilitates querying and modifying data, including operations like natural joins, aggregation, and set comparisons. Additionally, it covers the use of embedded SQL and dynamic SQL within programming languages.

Uploaded by

memsahni2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Introduction to SQL

Overview of the SQL Query Language

The SQL language has several parts:

• Data-definition language(DDL).TheSQL DDL provides commands for defining relation schemas,


deleting relations, and modifying relation schemas.

• Data-manipulation language (DML). The SQL DML provides the ability to query information from the
database and to insert tuples into, delete tuples from, and modify tuples in the database.

• Integrity. The SQL DDL includes commands for specifying integrity constraints that the data stored in
the database must satisfy. Updates that violate integrity constraints are disallowed.

• View definition. The SQL DDL includes commands for defining views.

• Transaction control. SQL includes commands for specifying the beginning


and ending of transactions.

• Embedded SQL and dynamic SQL. Embedded and dynamic SQL define how SQL statements can be
embedded within general-purpose programming languages, such as C, C++, and Java.

• Authorization.TheSQLDDLincludescommandsforspecifyingaccessrights to relations and views.

SQL Data Definition


The set of relations in a database must be specified to the system by means of a data-definition language (DDL).

The SQL DDL allows specification of not only a set of relations, but also information about each relation, including:
d -

Q



..

Queries on a Single Relation

Let us consider a simple query using our university


example, “Find the names of all instructors.” Instructor
names are found in the instructor relation, so we put
that relation in the from clause. The instructor’s name
appears in the name attribute, so we put that in the
select clause.

-
Now consider another query, “Find the department names of all instructors,” which can be written as:

SQL allows us to use the keyword all to specify explicitly that duplicates are
not removed:

· T
The Natural Join

Consider the query “For all instructors in the


university who have taught some course, find their
names and the course ID of all courses they taught”,
which we wrote earlier as:

This query can be written more concisely using the natural-join


operation in SQL as: The natural join of the instructor relation with the teaches relation.
9

Both of the above queries generate the same result.

For example, suppose we wish to answer the query “List the names of instructors along with the the titles of courses that they teach.”
The query can be written in SQL as follows:
The Rename Operation

Consider again the query that we used earlier:

The names of the attributes in the result are derived from the names of the attributes in the relations in the from clause.

SQL provides a way of renaming the attributes of a result relation. It uses the as clause, taking the form:
String Operations

Attribute Specification in Select Clause


-important
Basic Aggregation

Aggregation with Grouping


As an illustration, consider the query “Find the average salary in each department.”
We write this query as follows:
As another example of aggregation on groups of tuples, consider the query “Find the number of instructors in each
department who teach a course in the Spring 2010 semester.”
SQL allows testing tuples for membership in a relation. The in connective tests for set membership, where the set is a
collection of values produced by a select clause. The not in connective tests for the absence of set membership.

Set Comparison

As an example of the ability of a nested subquery to compare sets, consider the query “Find the names of all instructors
whose salary is greater than at least one instructor in the Biology department.”

Same

L
Let us find the names of all instructors that have a salary value greater than that of each instructor in the
Biology department. The construct > all corresponds to the phrase “greater than all.” Using this construct, we
write the query as follows:

.
The With Clause
Complex query for
with statement ,
-

You might also like