DB Final – Flashcards
Unlock all answers in this set
Unlock answersquestion
The ________ is the structure that contains descriptions of objects such as tables and views created by users.
A) SQL
B) schema
C) catalog
D) master view
answer
B) schema
question
The SQL command ________ defines a logical table from one or more tables or views.
A) create table
B) alter table
C) create view
D) create relationship
answer
C) create view
question
The first in a series of steps to follow when creating a table is to:
A) identify columns that must be unique.
B) identify each attribute and its characteristics.
C) create an index.
D) identify columns that must be null.
answer
B) identify each attribute and its characteristics.
question
What does the following SQL statement do?
Alter Table Customer_T
Add (Type Varchar (2));
A) Alters the Customer_T table to accept Type 2 Varchars
B) Alters the Customer_T table to be a Type 2 Varchar
C) Alters the Customer_T table, and adds a field called "Type"
D) Alters the Customer_T table by adding a 2-byte field called "Varchar"
answer
C) Alters the Customer_T table, and adds a field called "Type"
question
What does the following SQL command do?
insert into Customer_T values (001,'John Smith','231 West St','Boston','MA','02115');
A) Adds a new record to the Customer_T
B) Creates the Customer_T table
C) Deletes the Customer_T table
D) Updates the Customer_T table
answer
A) Adds a new record to the Customer_T
question
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')
A) It would work just fine.
B) You must specify the fields to insert if you are only inserting some of the fields.
C) There is no table keyword.
D) None of the above.
answer
B) You must specify the fields to insert if you are only inserting some of the fields.
question
What does the following SQL statement do?
Delete from Customer_T
where state = 'HI';
A) Deletes all records from customer_t where the state is equal to HI
B) Removes the Customer_T table from the database
C) Deletes all records from the Customer_T table
D) None of the above
answer
A) Deletes all records from customer_t where the state is equal to HI
question
What does the following SQL statement do?
Update Product_T
Set Unit_Price = 775
Where Product_ID = 7
A) Changes the price of a unit called Product_T to 7
B) Changes the unit price of Product 7 to 775
C) Changes the length of the Unit_Price field to 775
D) Updates the Product_T table to have a unit price of 775
answer
B) Changes the unit price of Product 7 to 775
question
In an SQL statement, which of the following parts states the conditions for row selection?
A) Select
B) From
C) Where
D) Group By
answer
C) Where
question
What does the following SQL statement do?
Select * From Customer Where Cust_Type = "Best"
A) Selects all the fields from the Customer table for each row with a customer labeled "Best"
B) Selects the "*" field from the Customer table for each row with a customer labeled "Best"
C) Selects fields with a "*" in them from the Customer table
D) Selects all the fields from the Customer table for each row with a customer labeled "*"
answer
A) Selects all the fields from the Customer table for each row with a customer labeled "Best"
question
What result will the following SQL statement produce?
Select Avg(standard_price) as average from Product_V;
A) The average of all products in Product_V
B) The average Standard_Price of all products in Product_V
C) The average price of all products
D) None of the above
answer
B) The average Standard_Price of all products in Product_V
question
Which of the following questions is answered by the SQL statement?
Select Count (Product_Description) from Product_T;
A) How many products are in the table Product_T?
B) How many products have product descriptions in the Product Table?
C) How many characters are in the field name "Product_Description"?
D) How many different columns named "Product_Description" are there in table Product_T?
answer
B) How many products have product descriptions in the Product Table?
question
What results will be produced by the following SQL query?
Select sum(standard_price) as Total_Price
from Product_V
where Product_Type = 'WOOD';
A) The total price of all products that are of type wood
B) The total price of all products
C) The Standard_Price of the first wood product in the table
D) The Standard_Price of any wood product in the table
answer
A) The total price of all products that are of type wood
question
Which of the following will produce the minimum of all standard prices?
A) Select standard_price from Product_V where Standard_Price = min;
B) Select min(standard_price) from Product_V;
C) Select Standard_Price from min(Product_V);
D) Select min(Standard_Price) from Product_V where Standard_Price = min(Standard_Price);
answer
B) Select min(standard_price) from Product_V;
question
What will result from the following SQL Select statement?
Select min(Product_Description)
from Product_V;
A) The minimum value of Product_Description will be displayed.
B) An error message will be generated.
C) The first product description alphabetically in Product_V will be shown.
D) None of the above.
answer
C) The first product description alphabetically in Product_V will be shown.
question
What result set will the following query return?
Select Item_No
from Order_V
where quantity > 10;
A) The Item_No of all orders that had more than 10 items
B) The Order_Id of all orders that had more than one item
C) The Order_Id of all orders that had more than 10 items
D) The Item_No of all orders that had 10 or more items
answer
A) The Item_No of all orders that had more than 10 items
question
What result set will the following query return?
Select Item_No, description
from item
where weight > 100 and weight < 200;
A) The Item_No and description for all items weighing less than 100
B) The Item_No for all items weighing between 101 and 199
C) The Item_No and description for all items weighing between 101 and 199
D) The Item_No for all items weighing more than 200
answer
C) The Item_No and description for all items weighing between 101 and 199
question
To eliminate duplicate rows in a query, the ________ qualifier is used in the SQL Select command.
A) alter
B) distinct
C) check
D) specific
answer
B) distinct
question
What result set is returned from the following query?
Select Customer_Name, telephone
from customers
where city in ('Boston','New York','Denver');
A) The Customer_Name and telephone of all customers
B) The Customer_Name and telephone of all customers living in either Boston, New York or Denver
C) The Customer_Name and telephone of all customers living in Boston and New York and
Denver
D) The Customer_Name of all customers living in Boston, New York or Denver
answer
B) The Customer_Name and telephone of all customers living in either Boston, New York or Denver
question
To get all the customers from Hawaii sorted together, which of the following would be used?
A) ORDER BY
B) GROUP BY
C) HAVING
D) SORT
answer
A) ORDER BY
question
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) A listing of all drivers, sorted by driver number
B) A listing of each driver as well as the number of deliveries that he or she has made
C) A count of all of the deliveries made by all drivers
D) None of the above
answer
B) A listing of each driver as well as the number of deliveries that he or she has made
question
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) A listing of all drivers who made deliveries to state = 'MA', sorted by driver number
B) 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
C) A count of all of the deliveries made to state = 'MA' by all drivers
D) None of the above
answer
B) 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
question
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) A listing of all drivers who made more than 2 deliveries as well as a count of the number of deliveries
B) A listing of all drivers
C) A listing of the number of deliveries greater than 2
D) A listing of all drivers who made more than 2 deliveries
answer
A) A listing of all drivers who made more than 2 deliveries as well as a count of the number of deliveries
question
Which of the following is true of the order in which SQL statements are evaluated?
A) The SELECT clause is always processed first.
B) The SELECT clause is always processed last.
C) The SELECT clause is processed before the ORDER BY clause.
D) The GROUP BY clause is processed before the WHERE clause.
answer
C) The SELECT clause is processed before the ORDER BY clause.
question
T/F: 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.
answer
TRUE
question
T/F: A major benefit of SQL as a standard is reduced training costs.
answer
TRUE
question
T/F: Applications can be moved from one machine to another when each machine uses SQL.
answer
TRUE
question
T/F: The SQL command used to populate tables is the INSERT command.
answer
TRUE
question
T/F: An insert command does not need to have the fields listed.
answer
TRUE
question
T/F: The WHERE clause includes the conditions for row selection within a single table or view and the conditions between tables or views for joining.
answer
TRUE
question
T/F: Expressions are mathematical manipulations of data in a table that may be included as part of the SELECT statement.
answer
TRUE
question
T/F: If multiple Boolean operators are used in an SQL statement, NOT is evaluated first, then AND, then OR.
answer
TRUE
question
T/F: Adding the DISTINCT keyword to a query eliminates duplicates.
answer
TRUE
question
T/F: The FROM clause is the first statement processed in an SQL command.
answer
TRUE
question
What are some of the advantages and disadvantages to an SQL standard?
answer
Some of the advantages are: reduced training costs, increased productivity, application portability, application longevity, reduced dependence on a single vendor and cross-system communication. Some disadvantages include: stifling creativity, difficulty in changing standard, and loss of application portability when adding additional proprietary features.
question
Explain the three classes of SQL commands and when they would be used.
answer
There are three classes or types of SQL commands. Data definition language commands are used to create tables, alter and drop tables, views and indexes. These commands are used to build the structure of the database as well as some additional objects. The next type is Data Manipulation Language commands, which are used to maintain and query a database. Commands in this class include select, update, delete and insert. The last type is the data control language commands, which are used to grant and revoke privileges on tables and other objects in the database.
question
What steps should be followed when preparing to create a table?
answer
When preparing to create a table, one should:
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
question
What three clauses are contained in most SQL retrieval statements?
answer
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.
question
Discuss when to use the GROUP BY clause.
answer
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.
question
How is the HAVING clause different from the WHERE clause?
answer
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. For example, if you had the following query:
select customer_id, sum(purchase_price*quantity)
from customer
where sum(purchase_price*quantity) > 100
this would not work. However, with a GROUP BY and HAVING written as follows we would get back all customers whose total purchases were greater than $100
select customer_id, sum(purchase_price*quantity)
from customer
group by customer_id
having sum(purchase_price*quantity) > 100
question
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):
A) equi-join.
B) natural join.
C) multivariate join.
D) inner join.
answer
B) natural join.
question
The most commonly used form of join operation is the:
A) outer join.
B) union join.
C) equi-join.
D) natural join.
answer
D) natural join.
question
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):
A) natural join.
B) equi-join.
C) outer join.
D) union join.
answer
C) outer join.
question
In which of the following situations would one have to use an outer join in order to obtain the desired results?
A) A report is desired that lists all customers who placed an order.
B) A report is desired that lists all customers and the total of their orders.
C) 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).
D) There is never a situation that requires only an outer join.
answer
C) 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).
question
One major advantage of the outer join is that:
A) information is easily accessible.
B) information is not lost.
C) the query is easier to write.
D) all of the above.
answer
B) information is not lost.
question
An operation to join a table to itself is called a:
A) sufficient-join.
B) inner join.
C) outer join.
D) self-join.
answer
D) self-join.
question
A type of query that is placed within a WHERE or HAVING clause of another query is called a:
A) master query.
B) subquery.
C) superquery.
D) multi-query
answer
B) subquery.
question
________ takes a value of true if a subquery returns an intermediate results table which contains one or more rows.
A) IN
B) HAVING
C) EXISTS
D) EXTENTS
answer
C) EXISTS
question
In SQL, a(n) ________ subquery is a type of subquery in which processing the inner query depends on data from the outer query.
A) correlated
B) paired
C) natural
D) inner
answer
A) correlated
question
The ________ clause is used to combine the output from multiple queries into a single result table.
A) INTERSECT
B) DIVIDE
C) COLLATE
D) UNION
answer
D) UNION
question
A ________ is a temporary table used in the FROM clause of an SQL query.
A) correlated subquery
B) derived table
C) view table
D) none of the above
answer
B) derived table
question
The UNION clause is used to:
A) combine the output from multiple queries into a single result table.
B) join two tables together to form one table.
C) find all rows that do not match in two tables.
D) none of the above.
answer
A) combine the output from multiple queries into a single result table.
question
All of the following are guidelines for better query design EXCEPT:
A) understand how indexes are used in query processing.
B) use a lot of self-joins.
C) write simple queries.
D) retrieve on the data that you need.
answer
B) use a lot of self-joins.
question
Explicit commands to manage transactions are needed when:
A) a transaction consists of just one SQL command.
B) multiple SQL commands must be run as part of a transaction.
C) autocommit is set to off.
D) none of the above.
answer
B) multiple SQL commands must be run as part of a transaction.
question
A procedure is:
A) stored within the database.
B) given a unique name.
C) called by name.
D) all of the above.
answer
D) all of the above.
question
T/F: The joining condition of an equi-join is based upon an equality.
answer
True
question
T/F: 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.
answer
True
question
T/F: 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');
answer
True
question
T/F: 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');
answer
False
question
T/F: 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.
answer
True
question
T/F: 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.
answer
True
question
T/F: 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);
answer
True
question
T/F: A correlated subquery is executed once for each iteration through the outer loop.
answer
True
question
T/F: The UNION clause is used to combine the output from multiple queries into a single result table.
answer
True
question
T/F: Figuring out what attributes you want in your query before you write the query will help with query writing.
answer
True
question
T/F: 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.
answer
True
question
T/F: SQL statements can be included in another language, such as C or Java.
answer
True
question
When is it better to use a subquery over using a join?
answer
Often, a subquery and join will return the same results. 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. Also, some queries (such as some not in queries) could not be done with joins.
question
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?
answer
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.
question
Explain how to combine queries using the UNION clause.
answer
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.
question
What strategies can be used to write queries that run more efficiently?
answer
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.