1 SQL II CIS*2450 Advanced Programming Concepts. 2 Data Types INTEGER –numbers without a decimal point –range is -2147483648 to 2147483647 SMALLINT –like.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

Concepts of Database Management Seventh Edition
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Database Systems: Design, Implementation, and Management Tenth Edition
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
Chapter 11 Group Functions
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Query-By-Example (QBE) 2440: 180 Database Concepts.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
Introduction to Oracle9i: SQL1 SQL Group Functions.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Structured Query Language Part I Chapter Three CIS 218.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Concepts of Database Management, 4th Edition, Pratt & Adamski
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
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.
Introduction to SQL J.-S. Chou Assistant Professor.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
Concepts of Database Management, Fifth Edition
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
Chapter 3 Single-Table Queries
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
Microsoft Access 2010 Chapter 7 Using SQL. Change the font or font size for SQL queries Create SQL queries Include fields in SQL queries Include simple.
Concepts of Database Management Seventh Edition
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
SQL (Chapter 2: Simple queries; Chapter 7 and 8: Nested and DML queries) Many of the examples in this document are based on the tables in the next slide.
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
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
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.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
SQL: Single Table Queries SELECT FROM WHERE ORDER D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 1.
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: 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.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
How to: SQL By: Sam Loch.
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL – Entire Select.
Chapter 4 Summary Query.
Structured Query Language
Reporting Aggregated Data Using the Group Functions
Section 4 - Sorting/Functions
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
Shelly Cashman: Microsoft Access 2016
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

1 SQL II CIS*2450 Advanced Programming Concepts

2 Data Types INTEGER –numbers without a decimal point –range is to SMALLINT –like INTEGER but smaller range to DECIMAL(p,q) –decimal numbers p digits long with q of these being decimal places $99, = DECIMAL(7,2)

3 Data Types CHAR(n) –character string n characters long VARCHAR(n) –string up to n characters long DATE –dates in the form DD-MON-YY or MM/DD/YYYY

4 Nulls The values for some columns may be unknown when a row is added to a table. SQL provides a special character referred to as a null data value or a null. –3 meanings: unknown, not applicable, missing In any system that supports null values, SQL allows one to choose whether or not to allow nulls for each column.

5 Nulls CREATE TABLE COMPUTER ( COMPID DECIMAL (2) NOT NULL, MFGNAME CHAR (15) NOT NULL, MFGMODEL CHAR (25), PROCTYPE DECIMAL (7,2), PRIMARY KEY(COMPID) ) integrity constraints

6 Comparison Operators Comparison Operators Meaning = Equal to < Less than > Greater than <= Less than or equal to >= Greater than or equal to <> or != Not equal to (implementation dependent)

7 Comparison Operators The BETWEEN feature allows one to specify a range of values without using the comparison operators. SELECT PACKID, PACKNAME, PACKCOST FROM PACKAGE WHERE PACKCOST BETWEEN 200 AND 400 SELECT PACKID, PACKNAME, PACKCOST FROM PACKAGE WHERE PACKCOST > 200 AND PACKCOST < 400

8 Compound Conditions Compound conditions are formed by connecting two or more simple statements using AND, OR, and NOT. Simple conditions connected by AND must all be TRUE for the compound statement to be TRUE.

9 Compound Conditions Simple conditions connected by OR: the compound statement is TRUE if any one (or both) of the simple conditions is TRUE. Preceding a condition by the word NOT reverses the truth of the original condition.

10 Compound Conditions SELECT PACKNAME FROM PACKAGE WHERE PACKTYPE <> 'Database' SELECT PACKNAME FROM PACKAGE WHERE NOT (PACKTYPE = 'Database')

11 Ordering The order of rows in a table is immaterial so one cannot predict the order of responses to queries. If the order is important then one can specifically request that the results be displayed in a desired order with the ORDER BY clause. SELECT EMPNUM, EMPNAME, EMPPHONE FROM EMPLOYEE ORDER BY EMPNAME

12 Ordering One can also sort on multiple keys and use descending order. The keys to be used are listed in order of importance after ORDER BY, and if this is followed by DESC then the ordering is descending. SELECT PACKID, PACKNAME, PACKTYPE, PACKCOST FROM PACKAGE ORDER BY PACKTYPE, PACKCOST DESC

13 Ordering ORDER BY PACKTYPE, PACKCOST DESC PACKIDPACKNAMEPACKTYPEPACKCOST 14OracleDatabase DB2Database AccessDatabase500 23Visual StudioIDE800 15C++BuilderIDE400

14 Aggregate Functions Built-in Function Meaning COUNT Number of rows satisfying the WHERE clause SUM Sum of the values in a column for all rows satisfying the WHERE clause AVG Average of the values in a column for all rows satisfying the WHERE clause

15 Aggregate Functions Built-in Function Meaning MAX Largest value in a column for all rows satisfying the WHERE clause MIN Smallest value in a column for all rows satisfying the WHERE clause

16 Aggregate Functions SELECT COUNT(PACKID), MAX(PACKCOST) FROM PACKAGE COUNT1 MAX

17 Aggregate Functions The AS operator is used in the following examples to name the resultant column.

18 Aggregate Functions SELECT COUNT(*) AS NUM_SUPPLIERS FROM SP NUM_SUPPLIERS 5 This counts the number of active suppliers since the COUNT(*) means count all the rows of SP –some suppliers may be duplicates!

19 Aggregate Functions SELECT COUNT(DISTINCT SP.SNO) AS NUM_SUPPLIERS FROM SP NUM_SUPPLIERS 4 More accurate: This counts the number of different suppliers supplying parts.

20 Aggregate Functions SELECT SUM(SP.QTY) AS QTY_P2 FROM SP WHERE SP.PNO = 'P2' This calculates the total quantity of part P2.

21 Aggregate Functions SELECT SP.PNO, SUM(SP.QTY) AS QTY FROM SP GROUP BY SP.PNO SP.PNOQTY P1600 P2400 P4400 GROUP BY creates groups based upon column entries. SUM adds within group. This produces a table of part numbers and their total quantities for each part supplied.

22 Nested Queries Can place one query inside another. The inner query is called a subquery and is executed first. SELECT PACKID, PACKNAME FROM PACKAGE WHERE PACKTYPE= 'Database‘ AND PACKCOST > ( SELECT AVG (PACKCOST) FROM PACKAGE WHERE PACKTYPE = 'Database' )

23 Client-Server and SQL An SQL-agent is an application which includes some SQL operations. The application is bound to an SQL-client which is used to communicate with an SQL-server. The server carries out the database operations. The client and server are connected through the CONNECT operation. After the transactions are completed, use the DISCONNECT statement.