SQL Training SQL Statements – Part 1. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Lesson Objectives Explain the role of SQL.

Slides:



Advertisements
Similar presentations
Basic SQL Introduction Presented by: Madhuri Bhogadi.
Advertisements

Virtual training week 4 structured query language (SQL)
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Database Lecture # 1 By Ubaid Ullah.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
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.
Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
 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.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
SQL Training Insert, Update & Delete. Insert, Update, Delete.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Single-Table Queries 1: Basics CS 320 Online. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Quick review of SQL And conversion to Oracle SQL.
Upgrading to SQL Server 2000 Kashef Mughal. Multiple Versions SQL Server 2000 supports multiple versions of SQL Server on the same machine It does that.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Oracle 11g DATABASE DEVELOPMENT LAB1. Introduction  Oracle 11g Database:-  Oracle 11g database is designed for some features, which helps to the organizations.
MySQL Database Management Systems Universitas Muhammadiyah Surakarta Yogiek Indra Kurniawan.
Information Building and Retrieval Using MySQL Track 3 : Basic Course in Database.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Access The L Line The Express Line to Learning 2007 L Line L © Wiley Publishing All Rights Reserved.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
SQL Training Join Statements. Joining Tables Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Relationships (Cardinalities) Most.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
CMPT 258 Database Systems The Relationship Model (Chapter 3)
SQL. Originally developed by IBM Standardized in 80’s by ANSI and ISO Language to access relational database and English-like non-procedural Predominant.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
1 A Very Brief Introduction to Relational Databases.
SQL Introduction to database and SQL. Chapter 1: Databases and Database Users 6 Introduction to Databases Databases touch all aspects of our lives. Examples:
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
1 Database Fundamentals Introduction to SQL. 2 SQL Overview Structured Query Language The standard for relational database management systems (RDBMS)
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
 CONACT UC:  Magnific training   
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.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
1 Copyright © 2005, Oracle. All rights reserved. Oracle Database Administration: Overview.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Fundamentals of DBMS Notes-1.
Web Systems & Technologies
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
CS 3630 Database Design and Implementation
Chapter 5 Introduction to SQL.
Introduction to Structured Query Language(SQL)
Structured Query Language (SQL) William Klingelsmith
CHAPTER 6: INTRODUCTION TO SQL
Chapter 8 Working with Databases and MySQL
مقدمة في قواعد البيانات
Database systems Lecture 3 – SQL + CRUD
SQL Fundamentals in Three Hours
Introduction To Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
Structured Query Language
IST 318 Database Administration
Presentation transcript:

SQL Training SQL Statements – Part 1

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Lesson Objectives Explain the role of SQL Write basic SQL Select statements with compound where clauses Page 2 At the end of this section you will be able to:

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Lesson Plan Page 3 What is SQL Select Statement Where Clause In, Like, Between Workshop

Structured Query Language

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Structured Query Language SQL is the language most commonly used to create and process data in relational databases. SQL can be used with Access, DB2, MySQL, Oracle, MS SQL Server, Sybase, or any other relational database. Data Control (DCL) Grant Revoke User Privileges Data Definition (DDL) Create Alter Drop Tables, Views, Constraints Data Manipulation (DML) Select Insert Update Delete Retrieve and Manipulate Data Page 5

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select Syntax SELECT field, field, field FROM table, table, view WHERE condition and condition or condition GROUP BY field, field, field HAVING … ORDER BY field asc SELECT customerName, billingCity, count(incidentID) FROM Customer, Incident WHERE customerID = reportedByCustomerID and billingCity = 'New York' GROUP BY customername, billingCity HAVING count(incidentID) > 50 ORDER BY customerName desc Page 6

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – All Rows Problem: Get a list of all the records and fields in the Fuelsource table. SELECT * FROM Fuelsource; Page 7

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – Specifying Columns Problem: Get a list of all the products that use propane (fuelsourceid = 3). select productcode, productdescription, productprice from product where fuelsourceid = 3 21 Rows Page 8

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select - Distinct Problem: What are the voltages of the products sold? SELECT DISTINCT voltage FROM Product Page 9

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select - Concatenation of Fields The Concatenation Function can be used to combine multiple fields into a single field. SELECT vendornamename, vendorfirstname || ' ' || vendorlastname as Name FROM vendor ORDER BY vendorname; Page 10

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Adding Where Clauses

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select - Where Condition The WHERE clause restricts the rows selected to those for which the condition is TRUE. If you omit this clause, Oracle returns all rows from the tables, views, or snapshots in the FROM clause. Examples: WHERE subscribedProductID > 5 WHERE city = 'New York' WHERE hourlyrate BETWEEN 60 and 80 WHERE subscribedProductName LIKE '%Shopper%' WHERE checkout IS NOT NULL WHERE customerID IN (2, 3, 6) Page 12

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – Where Predicate Problem: List of all vendors in California. SELECT vendorname FROM Vendor WHERE provinceabbreviation = ‘CA'; Page 13

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – Compound Where Problem: List of all the products that use Natural Gas, have a power rating of 7000 or 5000 and a frequency of 50. Page 14

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select - Null Predicate SELECT RequiredProductName, startEffectiveDate, endEffectiveDate FROM RequiredProduct WHERE endEffectiveDate IS NULL ORDER BY RequiredProductName; Problem: List all the RequiredProduct rows that do not have a endEffectiveDate. 161 Rows SELECT RequiredProductName, startEffectiveDate, endEffectiveDate FROM RequiredProduct WHERE endEffectiveDate IS NOT NULL ORDER BY RequiredProductName; 0 Rows Page 15

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – Between Predicate Problem: List products where the product price is between $ 7063 and $ Page 16

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – Between Predicate – Using Dates SELECT RequiredProductID, RequiredProductName,startEffectiveDate FROM RequiredProduct WHERE startEffectiveDate BETWEEN to_date('03/04/2004','MM/DD/YYYY') and to_date('06/01/2004', 'MM/DD/YYYY') ORDER BY startEffectiveDate; SUBSCRIBEDPRODUCTIDSUBSCRIBEDPRODUCTNAMESTARTEFFECTIVEDATE 42Secret Shopper4-Mar Market Decision Maker10-Apr Magazines10-Apr Corporate| Order Track2-May-04 50Television15-May Marketing Mix Management22-May-04 6 Rows Page 17

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – Like Predicate Problem: Select all rows in the Fuelsource table that have a description containing the word ‘Gas’. Page 18

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – Like Predicate Caution: Like Predicate is Case Sensitive. 1 Row SELECT RequiredProductID, RequiredProductName FROM RequiredProduct WHERE LOWER (RequiredProductName) Like '%web%'; SUBSCRIBEDPRODUCTIDSUBSCRIBEDPRODUCTNAME 125National Fast Affiliates - WEB file LOWERSyntax LOWER(char)Returns char, with all letters lowercase. UPPERSyntax UPPER(char) Returns char, with all letters uppercase. Page 19 Note: This function doesn’t work on some languages (Chinese). Not all SQL version support this function. The classroom server supports this function.

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – In Predicate Problem: List all Vendors in CT, PA, FL; Page 20

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Workshop

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Individual SQL Workshop 1 1.Using Oracle SQL Developer you will create the SQL statements required to produce the requested output. 2.You will begin by logging onto the training Database. 3.You have been provided with a list of frequent error messages. This is not a complete list and the resolution may not always one of the options listed as there are numerous causes for many of the errors. But these are the most frequent and will be helpful. Page 22

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Common Error Messages ORA-00918: column ambiguously defined If a column appears in multiple tables, you must qualify the fieldname with the table name. ORA-00933: SQL command not properly ended Look for missing single quotes around strings OR missing ‘and’ between Where clause statements OR Missing keywords such as Where, From, Group By, Order By ORA-00904: “String Expression": invalid identifier Make sure you are using single quotes and not double quotes. ORA-00904: “CUSTOMER"."CUSTOMERID": invalid identifier Make sure the table you are referencing is in the From clause OR Make sure the field you are referencing is in one of the tables in your From clause OR make sure you have spelled the table and/or field name correctly. Page 23

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Write the SQL to answer the business question. Only include the columns shown in the picture. Your answer should match the data shown. In some cases, only the first and last rows will be shown due to the size constraints of the page. Note: Oracle does not always print the entire column name – look at the Database Design for column names. Workshop Page 24

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. List all products in the Countries in the country table. Problem 1 – Simple Select Page 25

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Show all components with restockdaycount = Rows Problem 2 – Simple Select with Where Page 26

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. List all components with a weight between 18 and 30 ordered by the weight. 153 Rows Problem 3 – Select with Between Page 27

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. From the Vendor table, list all vendors and their userid when the vendor is in California or the userid is between 60 and 65. Problem 4 – Select with Compound Where Page 28

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. From the vendor table, list the information for provinceid = 5 (California) or = 32 (New York) or = 49 (Wisconsin) or = 35 (Ohio). Problem 5 – Select using the IN Predicate Page 29

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. List all the vendors that do not have a fax. Problem 6 – Select using NULL Predicate Page 30

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. From the Vendor table, list all the cities where there are vendors. Only list each city once. 30 Rows Problem 7 – Select using Distinct Page 31

Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. List all products with ‘9000’ or ‘7000’ in their name and the product name also has ‘110v’ as part of it. Problem 8 – Select using LIKE Predicate Page 32