SQL – Part I Yong Choi School of Business CSU, Bakersfield.

Slides:



Advertisements
Similar presentations
Yong Choi School of Business CSU, Bakersfield
Advertisements

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.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
SQL – Part II Yong Choi School of Business CSU, Bakersfield.
SQL – Part II Yong Choi School of Business CSU, Bakersfield.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
Concepts of Database Management, 4th Edition, Pratt & Adamski
Introduction to Structured Query Language (SQL)
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
Concepts of Database Management Sixth Edition
SQL (Standard Query Language) Yong Choi School of Business CSU, Bakersfield.
Chapter 3: SQL – Part I Yong Choi School of Business CSU, Bakersfield.
SQL – Part I Yong Choi School of Business CSU, Bakersfield.
Introduction to SQL J.-S. Chou Assistant Professor.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands 
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Introduction to SQL Yong Choi School of Business CSU, Bakersfield.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
ASP.NET Programming with C# and SQL Server First Edition
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Structured Query Language. SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database systems.
Concepts of Database Management Seventh Edition
Database A collection of related data. Database Applications Banking: all transactions Airlines: reservations, schedules Universities: registration, grades.
SQL – Part II Yong Choi School of Business CSU, Bakersfield.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
SQL – Part I Yong Choi School of Business CSU, Bakersfield.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Concepts of Database Management Seventh Edition
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Visual Programing SQL Overview Section 1.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
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.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
ADVANCED SQL.  The SQL ORDER BY Keyword  The ORDER BY keyword is used to sort the result-set by one or more columns.  The ORDER BY keyword sorts the.
SQL Structured Query Language. SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
3 A Guide to MySQL.
Web Systems & Technologies
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Chapter 5 Introduction to SQL.
Introduction to Structured Query Language(SQL)
Example 2 SELECT Customernum, CustomerName, Balance FROM Customer;
Yong Choi School of Business CSU, Bakersfield
Introduction To Structured Query Language (SQL)
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Yong Choi School of Business CSU, Bakersfield
Yong Choi School of Business CSU, Bakersfield
Introduction To Structured Query Language (SQL)
Structured Query Language
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Presentation transcript:

SQL – Part I Yong Choi School of Business CSU, Bakersfield

2 Study Objectives Understand the basic commands and functions of SQL Learn how SQL is used for data manipulation (to add, modify, delete, and retrieve data) Learn how to use SQL to query a database to extract useful information Learn how SQL is used for data administration (to create tables, indexes, and views) Practice SQL

3 Ideal Database Language Requirements Create database and table structures. –SQL has a data definition component that gives us the ability to meet this requirement. Manage the data component of the database. –SQL gives us a set of commands to add, update, and delete data within the database tables. Provide detailed data query capability. –"Standard" SQL uses a set of approximately thirty commands that allow us to retrieve data and to convert the raw data into useful information.

4 Introduction to SQL Standard Query Language (SQL) is the relational model’s standard language. The original version of SQL was developed at IBM's San Jose Research Laboratory. This language, originally called Sequel. The Sequel language has evolved since then, and its name has changed to SQL (Structured Query Language). In 1986, the American National Standards Institute (ANSI) published an SQL standard. –In 1992, work was completed on a significantly revised version of the SQL standard (SQL-92).

5 Introduction to SQL (con’t) SQL is relatively easy to learn –SQL commands set has a basic vocabulary of less than 100 words. SQL is a nonprocedural language. So, it is much easier to use. –Its user merely commands what is to be done without having to worry about how it's to be done. –Procedural language: COBOL, C, or Pascal.

6 More about SQL Basic data functions by SQL, and their basic SQL commands (detail on the class website): 1.Data definition through the use of CREATE 2.Data manipulation through INSERT, UPDATE, and DELETE 3.Data querying through the use of SELECT AND MANY OTHERS, which is the basis for all SQL queries. –We will try this first since we just completed all Access queries.

7 Basic Structure of SQL Queries SELECT FROM WHERE GROUP BY HAVING ORDER BY

8 The SELECT and FROM Statement The SELECT statement is used to select data from a table. The tabular result is stored in a result table (called the result set). The FROM statement is used to select tables. Syntax: –SELECT column_name(s) –FROM table_name To select all columns from a table, use a * symbol instead of column names: –SELECT * FROM table_name

9 The WHERE Statement To conditionally select data from a table, a WHERE clause can be added to the SELECT statement. Syntax: –SELECT column –FROM table –WHERE column operator value

10 Typical Data Types INTEGER: Numbers without a decimal point SMALLINT: Uses less space than INTEGER DECIMAL(p,q): P number of digits; q number of decimal places CHAR(n): Character string n places long DATE: Dates in DD-MON-YYYY or MM/DD/YYYY

11 Semicolon after SQL Statements? Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server. MS Access and SQL Server do not require to put a semicolon after each SQL statement, but other database SQLs force you to use it such as Oracle. So, you must use a semicolon for this class.Oracle –Access SQL commands are not case sensitive (including table and attribute names) but try to follow exact names for better readability. –Download SQL data file form the class web site.

12 CustomerNumCustomerNameStreetCityStateZipBalanceCreditLimitRepNum 148Al's Appliance and Sport 2837 Greenway FillmoreFL33336$6,550$7,50020 OrderNumPartNumNumOrderedQuotedPrice 21608AT9411$21.95 OrderNumOrderDateCustomerNum /20/ Customer OrderLine Order PartNumDescriptionOnHandClassWarehousePrice AT94Iron50HW3$24.95 Part Rep RepNumLastNameFirstNameStreetCityStateZipCommissionRate 20KaiserValerie624 RandallGroveFL33321$20,

13 Example 2 Example 2: Save as example 2 –List the number, name, and balance of all customers.

14 Example 2

15 Example 2 SELECT Customernum, CustomerName, Balance FROM Customer;

16 Example 3 Example 3: Save as example 3 –List the complete Part table. –Use of “ * “

17 Example 3

18 Example 3 SELECT * FROM Part;

19 Oracle 7.0 SQL Example SQL> select * from employee; EMP_ID EMP_LNAME EMP_FNAME EMP_SALARY EMP_DEPT_NO Kim John Johnson Steve Jonson Paul Lee Jim Basinger Jon Stone Sharon rows selected.

20 SQL Example – WHERE clause Example 4: Save as example 4 –List the name of every customers with $10,000 credit limit. Credit limit must be equal to $10000

21 Example 4 SQL Query with Where Condition

22 Example 4 SELECT Customername FROM Customer WHERE CreditLimit=10000;

23 SQL Example – WHERE clause Example 5: Save as example 5 –Find the name of customer 148.

24 Example 5 SQL Query to Find Customer 148

25 Example 5 SELECT Customer.CustomerNum FROM Customer WHERE Customer.CustomerNum='148';

26 SQL Comparison Operators For WHERE clause NOT Warehouse =‘3’ LIKE: LIKE ‘a*’, LIKE ‘*s’, Like ‘*Oxford*’ (NOT) BETWEEN AND (NOT) IN (123, 345)

27 SQL Examples Example 6: Save as example 6 –Find the customer name for every customer located in the city of Grove

28 Example 6

29 Example 6 SELECT Customername FROM Customer WHERE City = ‘Grove';

30 SQL Examples Example 7: Save as example 7 –List the number, name, credit limit, and balance for customers with credit limits that exceed their balances.

31 Example 7

32 Example 7 SELECT CustomerName, CustomerName, CreditLimit, Balance FROM Customer WHERE CreditLimit>Balance;

33 SQL Examples – Compound Conditions Example 8: Save as example 8 –List the description of all parts that are located in warehouse 3 and for which there are more than 20 units on hand.

34 Example 8 SQL Query with Compound Condition using ‘AND’

35 Example 8 SELECT Description FROM Part WHERE Warehouse=‘3’ AND OnHand>20;

36 SQL Examples – Compound Conditions Example 9: Save as example 9 –List the descriptions of all parts that are located in warehouse 3 or for which there are more than 20 units on hand.

37 Example 9 SQL Query using ‘OR’

38 Example 9 SELECT Description FROM Part WHERE Warehouse=‘3’ OR OnHand>20;

39 SQL Examples Example 10: Save as example 10 –List the description of all parts that are not in warehouse 3. –Use “NOT” (i.e., where NOT A = 100;)

40 Example 10 SQL Query using ‘NOT’

41 Example 10 SELECT Description FROM Part WHERE NOT Warehouse=‘3’;

42 SQL Examples Example 11: Save as example 11 –List the number, name, and balance of all customers with balances greater than or equal to $1,000 and less than or equal to $5,000. –(NOT) BETWEEN AND 78000

43 Example 11 Query with ‘BETWEEN’ Operator

44 Example 11 SELECT CustomerNum, CustomerName, Balance FROM Customer WHERE Balance BETWEEN 1000 AND 5000;

45 SQL Examples – Computed Field Computed field can involve: –addition(+), subtraction(-), Multiplication(*), or division (/) Example 12: Save as example 12 –List the number, name and available credit for all customers. –Use “AS” for assigning a new field name

46 Example 12 SQL Query with Computed Field

47 Example 12 SELECT CustomerNum, CustomerName, CreditLimit-Balance AS AvailableCredit FROM Customer;

48 SQL Examples – Computed Field Computed field can involve: –addition(+), subtraction(-), Multiplication(*), or division (/) Example 13: Save as example 13 –List the number, name, and available credit for all customers with credit limits that exceed their balances.

49 Example 13 SQL Query with Computed Field and Condition

50 Example 13 SELECT CustomerNum, CustomerName, CreditLimit-Balance AS AvailableCredit FROM Customer WHERE CreditLimit > Balance;

51 SQL Examples – LIKE and IN Example 14: Save as example 14 –List the number, name, and complete address of every customer located on a street that contain the letters “Oxford.” Customer names begin with B: Like B* Customer names end with E: Like *E Fine exact customer last name: like ‘*Choi*’

52 Example 14 SQL Query with ‘LIKE’ Operator

53 Example 14 SELECT CustomerNum, CustomerName, Street, City, State, Zip FROM Customer WHERE Street LIKE ‘*Oxford*’;

54 SQL Examples – LIKE and IN Example 15: Save as example 15 –List the number, name, and credit limit for every customer with a credit of $7,500, $10,000, or $15,000. –IN (7500, 10000, 15000);

55 Example 15 SQL Query with ‘IN’ Operator

56 Example 15 SELECT CustomerNum, CustomerName, CreditLimit FROM Customer WHERE CreditLimit IN (7500, 10000, 15000);

57 SQL Examples Default value of ORDER BY: ascending Example 16: Save as example 16 –List the number, name, and credit limit of all customers. Sort the customers by name in ascending order.

58 Example 16 SQL Query to Sort Data

59 Example 16 SELECT CustomerNum, CustomerName, CreditLimit FROM Customer ORDER BY CustomerName;

60 SQL Examples Default value of ORDER BY: ascending Example 17: Save as example 17 –List the number, name, and credit limit of all customers. Sort the customers by name (minor) in ascending order within credit limit (major) in descending order. –What about this case? customer name (minor) and credit limit (major)

61 Example 17 SQL Query to Sort on Multiple Fields

62 Example 17 SELECT CustomerNum, CustomerName, CreditLimit FROM Customer ORDER BY CreditLimit DESC, CustomerName;