ch7 Review Questions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

7.1. Describe the six clauses in the syntax of an SQL retrieval query.

Show what type of


constructs can be specified in each of the six clauses. Which of the six clauses are
required and which are optional?

The six clauses in the syntax of an SQL retrieval query.


1. SELECT < attribute list >
2. FROM < table list >
3. [ WHERE < condition > ]
4. [ GROUP BY < grouping attributes (S) > ]
5. [ HAVING < group condition > ]
6. [ ORDER BY < attribute list > ]

The SELECT statement: returns a result set of records from one or more tables
The FROM clause: is used to retrieve the desired data from the table for the provided
query.
The WHERE clause: is used to extract only those records that fulfill a specified
condition.
The GROUP BY Clause: is utilized in SQL with the SELECT statement to organize
similar data into groups. It combines the multiple records in single or more columns
using some functions.
HAVING Clause: utilized in SQL as a conditional Clause with GROUP BY Clause. This
conditional clause returns rows where aggregate function results matched with given
conditions only.
The ORDER BY clause: sorts the records in ascending order by default. To sort the
records in descending order, use the DESC keyword.

Required Clauses: SELECT and FROM


Optional clauses: WHERE, GROUP BY, HAVING and ORDER BY.

7.2. Describe conceptually how an SQL retrieval query will be executed by specifying
the conceptual order of executing each of the six clauses.

The SELECT clause lists the attributes to be retrieved. The FROM clause specifies all
relation needed in the query. The WHERE clause specifies the conditions for selection
of tuples from these relations, including join conditions if needed. GROUP BY specifies
the grouping of the attributes, HAVING specifies a condition on groups being selected
rather than individual tuples. ORDER BY specifies an order for displaying the result of a
query.

This study source was downloaded by 100000881699319 from CourseHero.com on 03-02-2024 19:22:45 GMT -06:00

https://www.coursehero.com/file/57653478/database-Hw5pdf/
7.3. Discuss how NULLs are treated in comparison operators in SQL. How are NULLs
treated when aggregate functions are applied in an SQL query? How are NULLs treated
if they exist in grouping attributes?

In SQL NULL values are treated as an UNKNOWN value, meaning nothing value, and it
has three logical operators TRUE, FALSE or UNKNOWN.

For comparison operators, NULL can be compared using IS or IS NOT operator.


For example: SELECT * FROM employees WHERE Last_Name IS NOT NULL;
This will return all records from the employees table where the Last Name of the record
does not contain a null value.

SQL treats each NULL as a distinct value, so these relational operators =, <,> cannot be
used for comparison. In general, NULL values are discarded when aggregate functions
are applied to a particular column.

If NULL exists in the grouping attribute, then a separate group is created for all tuples
with a NULL value in the grouping attribute.

7.4. Discuss how each of the following constructs is used in SQL, and discuss the
various options for each construct. Specify what each construct is useful for.

Nested queries: Also known as subquery or inner query; it is a query that is nested
inside a SELECT, INSERT, UPDATE, or DELETE clauses or even in another nested
query, and can be used anywhere as long the expression allows it.
Useful for queries that require existing values in the database be fetched and then used
in a comparison condition

Joined tables and outer joins: A joined table is the resultant that is generated by an
inner join, outer join or a cross join. Where the outer joins are classified in: Left outer
join, Right outer join and, Full outer join.
Joined tables are useful when information from multiple tables need to be used for a
query. It is easier to join these tables together first and then query them for the desired
information.

This study source was downloaded by 100000881699319 from CourseHero.com on 03-02-2024 19:22:45 GMT -06:00

https://www.coursehero.com/file/57653478/database-Hw5pdf/
Aggregate functions and Grouping:
Aggregate functions: It is a function that performs a calculation on a set of values, and
returns a single value, the most used aggregate function are: AVG, COUNT, MIN, MAX
and SUM, but there are more.
Aggregate functions are used to perform mathematics operations easily to the
information we have in tables such as finding the min, max, sum, count, or average of a
certain value in a specific table.
Grouping: In most cases to subgroup the tuples in a relation the aggregation function
needs to be applied. These subgroups are dependent on some attribute values. On
applying the GROUP BY clause the table is divided into a different group.
The GROUP BY clause is useful when there is a need of dividing the table into different
groups according to the attributes values.

Triggers:
A SQL trigger will automatically execute when an event like (INSERT, DELETE or
UPDATE) occurs, as long as it has been previously set for any of these statements to
fire the trigger. This creates "active database" behavior.
Trigger can be used for the following purpose:

1. To create some derived column automatically.


2. To improve security authorization.
3. To avoid the invalid transaction

Assertions and how they differ from triggers:


An assertion is a piece of SQL code which makes sure a condition is satisfied, allowing
the specification of a more general constraint which must always be evaluated to true,
or it stops the actions being taken for all database entries.
A trigger as explained before is a piece of SQL code to execute either before or after an
update, insert, or delete statement in a database.

Triggers:
Triggers are more powerful because the can check conditions and modify the data.
Triggers are linked to specific tables and specific events.

Assertions:
Assertions do not modify the data, they only check certain conditions.
Assertions are not linked to specific tables in the database and not linked to specific
events.

It is used only to check the condition of the database schema, as it is not explicitly tied
to any event.

This study source was downloaded by 100000881699319 from CourseHero.com on 03-02-2024 19:22:45 GMT -06:00

https://www.coursehero.com/file/57653478/database-Hw5pdf/
The SQL WITH clause: The SQL WITH clause allows you to give a sub-query block a
name, which can be referenced in several places within the main SQL query. It is
sometime similar like creating a view that will be used in a query then drop.
The SQL WITH clause was introduced by Oracle in the 9i release 2, that is why is not
supported by all database system.

It can be used to create a complex statement rather than multiple simple statements. It
can be used as well to break down complex SQL queries with which it easy for
debugging and processing.

SQL CASE construct: The CASE construct statement goes through different conditions
and returns a value when the first condition is met (like the same IF-THEN-ELSE
statement in JAVA). So, once a condition is true, it will stop reading and return the
result. If no conditions are true, it returns the value in the ELSE clause. If there is no
ELSE part and no conditions are true, it returns NULL.

It can be used to perform an operation when a particular condition occurs.

Views and their updatability: In SQL, a view is a virtual table based on the result-set of
an SQL statement. Basically, the CREATE VIEW statement is a way of abstracting the
complexities of a particular query logic and save it as metadata, so other people can
build their queries off that query logic, which simplify things a ton to the end-user.
A view contains rows and columns, just like a real table. The fields in a view are fields
from one or more real tables in the database. We can add SQL functions, WHERE, and
JOIN statements to a view and present the data as if the data were coming from one
single table.
The power of creating views for our tables is extremely useful when the table need to be
referenced frequently, so instead of writing complex queries every time we would like to
obtain the same result for different tables, we simply called the view object and it will
show us the result stored in the virtual table for that specific table.

This study source was downloaded by 100000881699319 from CourseHero.com on 03-02-2024 19:22:45 GMT -06:00

https://www.coursehero.com/file/57653478/database-Hw5pdf/
Schema change commands: The schema change command is used in SQL to alter a
schema by adding or dropping the attributes, table, constraints and other schema
elements. This can be done when the database does not require to compile the
database schema again and the database is optional.

There are two Schema change Commands:

DROP command: It is used to drop named schema elements, such as tables, domains,
types, or constraints. The whole schema if it is no longer needed can be dropped by
using the DROP SCHEMA command.

There are two drop behavior options: CASCADE and RESTRICT.

The CASCADE option is used to remove the database schema and all its tables,
domains, and other elements. While we use the RESTRICT option to drop the schema
only it this one does not have any elements in it, otherwise, the DROP command will not
be executed.

ALTER command: The schema can be change with the help of the Alter command,
such as changing the column name, adding attributes, renaming the schema or
specifying a new owner.

It can be used to change the schema or to drop the schema.

This study source was downloaded by 100000881699319 from CourseHero.com on 03-02-2024 19:22:45 GMT -06:00

https://www.coursehero.com/file/57653478/database-Hw5pdf/
Powered by TCPDF (www.tcpdf.org)

You might also like