CGS 2545 Final
What steps should be followed when preparing to create a table?
1. Identify the appropriate data type and length for each attribute 2. Identify the columns that should accept null values 3. Identify that columns that need to be unique 4. Identify all primary-foreign key mates 5. Determine default values 6. Identify any columns for which domain constraints, such as check, need to be stated 7. Create the table and any indexes using create table and create index statements
What is a derived table? When is it used? Can you describe any situations where you would have to use it over a subquery in the WHERE clause?
A derived table can be created by placing a subquery within the FROM clause. The derived table is simply a temporary table in memory which can be accessed just like a table or view. Sometimes, these are referred to as inline views. A derived table is useful for situations where you need to create aggregate values (such as a sum) and then use these in another query. A derived table would be used instead of a subquery in cases where you need to display results from multiple tables. If the results need to come from a subselect, this is not possible, so we would have to use a derived table
What will be returned when the following SQL query is executed? Select driver_no, count(*) as num_deliveries from deliveries group by driver_no having count(*) > 2;
A listing of all drivers who made more than 2 deliveries as well as a count of the number of deliveries
What will be returned when the following SQL statement is executed? Select driver_no,count(*) as num_deliveries from deliveries group by driver_no;
A listing of each driver as well as the number of deliveries that he or she has made
What will be returned when the following SQL statement is executed? Select driver_no, count(*) as num_deliveries from deliveries where state = 'MA' group by driver_no;
A listing of each driver who made deliveries to state = 'MA' as well as the number of deliveries that each driver has made to that state
• In which of the following situations would one have to use an outer join in order to obtain the desired results?
A report is desired that lists all customers and the total of their orders during the most recent month, and includes customers who did not place an order during the month (their total will be zero).
What does the following SQL command do? insert into Customer_T values (001,'John Smith','231 West St','Boston','MA','02115');
Adds a new record to the Customer_T
What does the following SQL statement do? Alter Table Customer_T Add (Type Varchar (2));
Alters the Customer_T table, and adds a field called "Type"
What does the following SQL statement do? Update Product_T Set Unit_Price = 775 Where Product_ID = 7
Changes the unit price of Product 7 to 775
Explain the three classes of SQL commands and when they would be used.
Data definition language- commands are used to create tables, alter and drop tables, views and indexes. Used to build structure of database. Data Manipulation Language- commands used to maintain and query a database. Include Select, update, delete and insert. Data control language- commands used to grant and revoke privileges on tables and other objects in
What does the following SQL statement do? Delete from Customer_T where state = 'HI';
Deletes all records from customer_t where the state is equal to HI
________ takes a value of true if a subquery returns an intermediate results table which contains one or more rows.
EXISTS
The following query will execute without errors. select customer.customer_name, salesman.sales_quota from customer where customer.salesman_id = (select salesman_id where lname = 'SMITH');
FALSE
Which of the following questions is answered by the SQL statement? Select Count (Product_Description) from Product_T;
How many products have product descriptions in the Product Table?
What strategies can be used to write queries that run more efficiently?
It makes sense to specify the attribute names rather than just * in the SELECT clause. If you are writing a query for a wide table, specifying only the attributes that are needed is going to save a great deal of processing time. Also, changes in the base table post-production could affect query results. Specifying the attribute names will make it easier to notice and correct for such errors. Use subqueries sparingly, since these increase processing time. If you have several separate reports which need data from one table, write one query to retrieve all of the data and use what you need in each report.
When is it better to use a subquery over using a join?
Joining is most useful when data from several tables needs to be retrieved and there is no nesting. When relationships are nested, it is best to use a subquery.
The most commonly used form of join operation is the:
Natural Join
To get all the customers from Hawaii sorted together, which of the following would be used?
ORDER BY
Which of the following will produce the minimum of all standard prices?
Select min(standard_price) from Product_V;
What does the following SQL statement do? Select * From Customer Where Cust_Type = "Best"
Selects all the fields from the Customer table for each row with a customer labeled "Best"
A correlated subquery is executed once for each iteration through the outer loop.
TRUE
A major benefit of SQL as a standard is reduced training costs.
TRUE
A natural join is the same as an equi-join, except that it is performed over matching columns that have been defined with the same name, and one of the duplicate columns is eliminated.
TRUE
A transaction is the complete set of closely related update commands that must all be done, or none of them done, for the database to remain valid.
TRUE
Adding the DISTINCT keyword to a query eliminates duplicates
TRUE
An insert command does not need to have the fields listed.
TRUE
Applications can be moved from one machine to another when each machine uses SQL
TRUE
Expressions are mathematical manipulations of data in a table that may be included as part of the SELECT statement.
TRUE
Figuring out what attributes you want in your query before you write the query will help with query writing.
TRUE
If multiple Boolean operators are used in an SQL statement, NOT is evaluated first, then AND, then OR.
TRUE
In order to find out what customers have not placed an order for a particular item, one might use the NOT qualifier along with the IN qualifier.
TRUE
One of the original purposes of the SQL standard was to provide a vehicle for portability of database definition and application modules between conforming DBMSs.
TRUE
SQL statements can be included in another language, such as C or Java.
TRUE
The FROM clause is the first statement processed in an SQL command.
TRUE
The SQL command used to populate tables is the INSERT command.
TRUE
The UNION clause is used to combine the output from multiple queries into a single result table
TRUE
The WHERE clause includes the conditions for row selection within a single table or view and the conditions between tables or views for joining.
TRUE
The following SQL statement is an example of a correlated subquery. select first_name, last_name, total_sales from salesman s1 where total_sales > all (select total_sales from salesman s2 where s1.salesman_id != s2.salesman_id);
TRUE
The following queries produce the same results. select customer_name, customer_city from customer, salesman where customer.salesman_id = salesman.salesman_id and salesman.lname = 'SMITH'; select customer_name, customer_city from customer where customer.salesman_id = (select salesman_id from salesman where lname = 'SMITH');
TRUE
The joining condition of an equi-join is based upon an equality
TRUE
When EXISTS or NOT EXISTS is used in a subquery, the select list of the subquery will usually just select all columns as a placeholder because it doesn't matter which columns are returned.
TRUE
What result set is returned from the following query? Select Customer_Name, telephone from customers where city in ('Boston','New York','Denver');
The Customer_Name and telephone of all customers living in either Boston, New York or Denver
Discuss when to use the GROUP BY clause.
The GROUP BY clause is useful when you have a set of values for one column (such as a salesperson ID) and you would like to then calculate something like total sales for each salesperson. Rather than having to use the sum function with one salesperson's ID in the WHERE clause (and run multiple queries) you can use the GROUP BY to get the same results in one query.
What result set will the following query return? Select Item_No, description from item where weight > 100 and weight < 200;
The Item_No and description for all items weighing between 101 and 199
What result set will the following query return? Select Item_No from Order_V where quantity > 10;
The Item_No of all orders that had more than 10 items
Which of the following is true of the order in which SQL statements are evaluated?
The SELECT clause is processed before the ORDER BY clause.
What three clauses are contained in most SQL retrieval statements?
The SELECT clause, which lists the columns and calculated expression from base tables. The FROM clause, which identifies tables and views which we want to gather data from in the query. Finally, the WHERE clause, which is used to specify conditions for selection of rows in the result set.
Explain how to combine queries using the UNION clause.
The UNION clause is used to combine the output of multiple queries into one set of output. In order for the union to work, each query must return the same number of columns. Also, each output column must be union compatible, meaning it is of the same type. The CAST command can be used to convert from one datatype to another if need be. If you wish to order the output rows, you must specify an ORDER BY clause in the last query
What result will the following SQL statement produce? Select Avg(standard_price) as average from Product_V;
The average Standard_Price of all products in Product_V
What will result from the following SQL Select statement? Select min(Product_Description) from Product_V;
The first product description alphabetically in Product_V will be shown.
What results will be produced by the following SQL query? Select sum(standard_price) as Total_Price from Product_V where Product_Type = 'WOOD';
The total price of all products that are of type wood
The ________ clause is used to combine the output from multiple queries into a single result table.
UNION
In an SQL statement, which of the following parts states the conditions for row selection?
Where
How is the HAVING clause different from the WHERE clause?
While the WHERE clause works on each row in a query resultset, the HAVING clause works on the aggregate (or combined) rows in a GROUP BY. WHERE does not allow aggregates while the HAVING does allow aggregates.
Given a table named store with 5 fields: store_id, address, city, state, zipcode, why would the following insert command not work? insert into store values ('234 Park Street')
You must specify the fields to insert if you are only inserting some of the fields.
A procedure is:
all of the above (stored within the database. given a unique name. called by name. )
The UNION clause is used to:
combine the output from multiple queries into a single result table.
In SQL, a(n) ________ subquery is a type of subquery in which processing the inner query depends on data from the outer query.
correlated
The SQL command ________ defines a logical table from one or more tables or views.
create view
A ________ is a temporary table used in the FROM clause of an SQL query.
derived table
To eliminate duplicate rows in a query, the ________ qualifier is used in the SQL Select command.
distinct
The first in a series of steps to follow when creating a table is to:
identify each attribute and its characteristics.
One major advantage of the outer join is that:
information is not lost
Explicit commands to manage transactions are needed when:
multiple SQL commands must be run as part of a transaction.
A join that is based upon equality between values in two common columns with the same name and where one duplicate column has been removed is called a(n):
natural join.
A join in which rows that do not have matching values in common columns are still included in the result table is called a(n):
outer join.
What are some of the advantages to an SQL standard?
reduced training costs, increased productivity, application portability, application longevity, reduced dependence on a single vendor and cross-system communication
The ________ is the structure that contains descriptions of objects such as tables and views created by users.
schema
An operation to join a table to itself is called a:
self-join
What are some of the disadvantages to an SQL standard?
stifling creativity, difficulty in changing standard, and loss of application portability when adding additional proprietary features.
A type of query that is placed within a WHERE or HAVING clause of another query is called a:
subquery.