Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Database Exercises Fall, 2009.

Similar presentations


Presentation on theme: "The Database Exercises Fall, 2009."— Presentation transcript:

1 The Database Exercises Fall, 2009

2

3 Use the database presented in the previous slide and write the SQL statements to answer the following questions.

4 FORMAT for Select statement
Basic format SELECT attribute list FROM table Just list a series of attributes from one table SELECT table.attribute, table.attribute FROM table; List of attributes from many tables SELECT table.attribute, table2.attribute, table2.attribute FROM table, table2

5 Derived Attributes You can use the SELECT statement to build a set of DERIVED ATTRIBUTE values Example SELECT table.attribute, table.attribute/2 as “MY TOTAL” FROM table; In this example table.attribute/2 would perform a calculation and display the results under a column headed MY TOTAL.

6 ORDER BY CLAUSE The ORDER BY clause allows you to sort the information. You list the attributes in order by the MAIN SORT, NEXT SORT, NEXT SORT, etc. For each sort, the ordering is assumed to be ascending. If you wish to reverse the order, you add the clause DESC after the attribute.

7 EXAMPLES of ORDER BY SELECT table.attribute, table.attribute2 FROM table ORDER BY table.attribute This sorts the table in ascending order. SELECT table.attribute, table.attribute2 FROM table ORDER BY table.attribute DESC By adding the DESC after the attribute, the sorts for the table changes to descending order.

8 WHERE CLAUSE The WHERE clause can be used to limit the records from the table that are used in the select. The WHERE clause has a condition after the clause that limits the records used in the select. The condition is usually a comparison of an attribute with a specific value.

9 Types of comparisons Operator Format Example > Greater Than
attribute > value SALARY > 1000 < Less than attribute < value SALARY < 1000 = Equal to attribute = value SALARY = 1000 >= Greater than/Equal to attribute >= value SALARY >= 1000 <= less than/equal to attribute <= value SALARY <= 1000 <> Not equal attribute <> value SALARY <> 1000

10 Examples of WHERE statement
Find all the people whose salary is greater than 1000. SELECT table.attribute, table.attribute FROM table WHERE table.attribute > value;

11 Several Selection Criteria
You may use the where clause with multiple criteria. You connect the criteria using AND, OR As the connectors. If the record MUST satisfy BOTH criteria, use AND. If the record MUST satisfy only ONE of all the criteria use OR.

12 WHERE CLAUSE with SEVERAL CRITERIA
SELECT table.attribute, table. attribute FROM table WHERE table.attribute = value AND table.attribute2 = value; Here the record needs to satisfy BOTH clauses in order to be displayed. SELECT table.attribute, table. attribute FROM table WHERE table.attribute = value OR table.attribute2 = value; Here the record needs to satisfy ONLY one of the clauses in order to be displayed.

13 COMBINING TABLES WHERE clauses may be used to join several tables.
You should use the relationships view to combine the tables. When you write the criteria, you should connect the tables using the WHERE clause

14 EXAMPLE of COMBINING tables
SELECT table.attribute1, table2.attribute2 from table, table2 WHERE table.attribute = table2.attribute; In this example there should be a relationship between table and table2—with the PK for one table pointing to the FK which is listed in the other table.

15 AGGREGATE FUNCTIONS You can use select statements to collect columns information and perform calculations. Examples of function types AVG (average of the numeric values) SUM (total of the numeric values) MIN (lowest of the values) MAX (highest of the values) COUNT (total the number of records)

16 GROUP BY clause If you use the aggregate function, you need to include a GROUP BY clause with the attribute used in the function Example SELECT COUNT(table.attribute) FROM table GROUP BY table.attribute;

17 Adding information to DB
You can enter data into the database without using the tables list. Use the INSERT command INSERT INTO table (attribute1, attribute2) VALUES (“for1”, “for2”); This will insert a record into the table and put for1 into attribute1 and for2 into attribute2

18 Removing Records You can remove records from the database by using the DELETE commend Format: DELETE FROM table [where xxx] This command usually includes a WHERE clause to limit the records which are deleted.

19 Changing Record Values
You can change the values in one or more records UPDATE tablename SET table.attribute TO “newvalue”; This statement may include a WHERE clause to limit the records which are changed.

20 Select statements List the name and the hire date for all the employees. List each DISTINCT charter destination and the distance of the charter. For all charters, longer than 1000 miles, list the charter destination, the number of the aircraft and the model of the aircraft. For each aircraft, find the number of seats in the aircraft.

21 Aggregate Functions Find the average distance of any charter.
Find the total distance of all charters. How many customers owe a balance? What is the total balance owed by all the customers? (This should be a single value) How many customers do no owe any balance? How many charters does any aircraft fly? How many crew members are on each charter?

22 Changes to the Database
Add yourself to the CUSTOMER table. Give yourself a balance of $405.50 Change the balance for everyone in the CUSTOMER table to be 10% less than the current value (Hint: set balance to .9*balance)….. Now for everyone whose Balance is less than $200.00, set it to zero. Delete yourself from the list of customers.


Download ppt "The Database Exercises Fall, 2009."

Similar presentations


Ads by Google