The Database Exercises Fall, 2009.

Slides:



Advertisements
Similar presentations
Structured Query Language (SQL)
Advertisements

Concepts of Database Management Seventh Edition
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Query-By-Example (QBE) 2440: 180 Database Concepts.
Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.
SQL Neyha Amar CS 157A, Fall Inserting The insert statement is used to add a row of data into a table Strings should be enclosed in single quotes,
Structured Query Language Part I Chapter Three CIS 218.
Concepts of Database Management Sixth Edition
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Microsoft Access 2010 Chapter 7 Using SQL.
Concepts of Database Management, Fifth Edition
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 3 Single-Table Queries
INLS 623– S QL Instructor: Jason Carter. SQL SELECT DISTINCT SELECT DISTINCT column_name, column_name FROM table_name ;
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Concepts of Database Management Seventh Edition
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Querying a Database - A question or an inquiry (dictionary.com) - WHAT ARE WE ASKING QUESTIONS ABOUT? THE DATA - BY ASKING QUESTIONS OF THE DATA WE OBTAIN?
Information Technologies and Microsoft SQL Server Day 2 by Alper Özpınar
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
BACS 287 Structured Query Language 1. BACS 287 Visual Basic Table Access Visual Basic provides 2 mechanisms to access data in tables: – Record-at-a-time.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
MIS2502: Data Analytics SQL – Getting Information Out of a Database.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
SQL Query Getting to the data ……..
MySQL DML Commands By Prof. B.A.Khivsara
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
CS580 Advanced Database Topics
(SQL) Aggregating Data Using Group Functions
GO! with Microsoft® Access e
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Structured Query Language
MENAMPILKAN DATA DARI SATU TABEL (Chap 2)
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Aggregations Various Aggregation Functions GROUP BY HAVING.
Chapter 4 Summary Query.
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Aggregating Data Using Group Functions
Database systems Lecture 3 – SQL + CRUD
Access: SQL Participation Project
Structured Query Language
Structured Query Language – The Fundamentals
Reporting Aggregated Data Using the Group Functions
Section 4 - Sorting/Functions
Projecting output in MySql
Joins and other advanced Queries
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
Manipulating Data Lesson 3.
Query-by-Example Transparencies
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

The Database Exercises Fall, 2009

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

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

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.

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.

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.

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.

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

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;

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.

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.

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

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.

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)

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;

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

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.

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.

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.

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?

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.