Single-Table Queries 1: Basics CS 320 Online. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects.

Slides:



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

Multiple Table Queries 2: Outer Joins, Self Joins, Nested Queries, and Views CS 320.
Action Queries CS 320. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects  Data Manipulation Language.
Introduction to Structured Query Language (SQL)
Structured query language This is a presentation by JOSEPH ESTRada on the beauty of Structured Query Language.
Structured Query Language Part I Chapter Three CIS 218.
Creating Database Tables CS 320. Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database.
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
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
Microsoft Access 2010 Chapter 7 Using SQL.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Using SQL Queries to Insert, Update, Delete, and View Data Date Retrieval from a single table & Calculations © Abdou Illia MIS Spring 2015.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
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.
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.
Working with Tables: Data Management and Retrieval Dr. Bernard Chen Ph.D. University of Central Arkansas.
SQL Training SQL Statements – Part 1. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Lesson Objectives Explain the role of SQL.
Concepts of Database Management Seventh Edition
Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
SQL (DDL & DML Commands)
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
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
ADVANCED SQL SELECT QUERIES CS 260 Database Systems.
SQL SELECT QUERIES CS 260 Database Systems. Overview  Introduction to SQL  Single-table select queries  Using the DBMS to manipulate data output 
Creating PHPs to Insert, Update, and Delete Data CS 320.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
SQL - DML. Data Manipulation Language(DML) Are used for managing data: –SELECT retrieve data from the a database –INSERT insert data into a table –UPDATE.
SQL Unit – 2 Base Knowledge Presented By Mr. R.Aravindhan.
Information Building and Retrieval Using MySQL Track 3 : Basic Course in Database.
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.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
Introduction to Databases Queries CS 146. Sample Database: CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.
Database UpdatestMyn1 Database Updates SQL is a complete data manipulation language that can be used for modifying the data in the database as well as.
1 Creating and Maintaining Database Objects Part 1 Database Systems.
SQL ACTION QUERIES AND TRANSACTION CONTROL CS 260 Database Systems.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Sql DDL queries CS 260 Database Systems.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
SQL Statements: Queries. Relational Databases Relational Databases organize the data in tables with rows and columns. This is similar to the organization.
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 6 CGI/Perl and databases.
Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from a single table  Usually queries combine data from.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Introduction to Databases Queries CS 146. Sample Database: CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
3 A Guide to MySQL.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
Chapter 5 Introduction to SQL.
Advanced Accounting Information Systems
Introduction to Oracle9i: SQL
Structured Query Language
SQL Queries Chapter No 3.
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Single-Table Queries 1: Basics CS 320 Online

Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects  Data Manipulation Language (DML)  Used to insert, update, delete, and view the data in database objects

 Query: question represented in a way a database can understand  SQL query components:  SELECT: columns whose values you want to retrieve  FROM: tables that contain the columns in the SELECT clause  WHERE: optional search condition to restrict which records are retrieved Database Queries

Programming Note: Before you can run SELECT queries, your database must contains data Run the scripts to insert the records in the CANDY database

CANDY database tables (with revised field names) CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT

Retrieving Data From a Single Table Basic syntax: SELECT field1, field2, … FROM tablename WHERE search_condition SELECT cust_id, cust_name FROM candy_customer WHERE cust_id = 1

Retrieving All Fields or All Records  To retrieve all fields in the table: use the "*" wildcard character  To retrieve all records in a table: omit the search condition SELECT * FROM tablename WHERE search_condition

Examples of Queries that Retrieve all Fields and all Records SELECT * FROM candy_purchase; SELECT * FROM candy_cust_type;

Search Conditions General format:  Operators: =,, =, <> or !=, BETWEEN, IN, LIKE Examples: FieldName Operator TargetValue prod_id = 1 purch_pounds > 5 prod_cost >= 9.99 purch_status != 'PAID' purch_pounds BETWEEN 5 AND 10 prod_id IN (1, 3, 5)

SearchTarget Values Numbers  Just type the number Text strings  Enclose in single or double quotes  Search for strings with embedded single quotes by typing \' or \" Dates  Enter as a text string in 'yyyy-mm-dd' format prod_id = 1 prod_cost >= 9.99 cust_name = 'Jones, Joe' prod_desc = 'Millionaire\'s Macadamia Mix' purch_date = ' '

Using IN and BETWEEN IN: retrieves all matching values within a set of values BETWEEN: retrieves all matching values within a range of values (inclusive) WHERE cust_zip IN ('91211', '91212') WHERE prod_id BETWEEN 1 AND 3

Partial-text search: searches for part of a string within a character field  Use the % wildcard character to match 0 or more characters  Examples: Partial-Text Searches Using LIKE WHERE cust_zip LIKE '9121%' WHERE cust_name LIKE '%s' WHERE cust_name LIKE '%s%'

Searching for NULL Values NULL: undefined Search conditions for NULL and non- NULL values: WHERE fieldname IS NULL WHERE fieldname IS NOT NULL WHERE cust_phone IS NULL WHERE purch_delivery_date IS NOT NULL

Compound Search Conditions  Formed by connecting two or more search conditions using the AND or OR operator  AND: query only retrieves records for which both conditions are true  OR: query retrieves records for which either condition is true

Example Compound Search Conditions WHERE Condition1 AND Condition2 WHERE Condition1 OR Condition2 WHERE prod_id = 1 AND purch_date = ' ' WHERE prod_id = 1 OR prod_id = 3

Using AND and OR in Search Conditions Every expression must be well-formed:  Do this:  Not this: WHERE purch_date > ' ' AND purch_date < ' ' WHERE purch_date > ' ' AND < ' '

MySQL evaluates AND expressions first, then OR expressions To force a specific evaluation order, place conditions to be evaluated first in parentheses! Order of AND/OR Evaluation SELECT cust_id FROM candy_customer WHERE cust_type = 'W' AND (cust_zip = '91209' OR cust_zip = '91212')

Test Yourself: How many fields and how many records will the following query retrieve? a. 7 fields and 14 records b. 14 fields and 7 records c. 7 fields and 9 records d. None of the above SELECT * FROM candy_purchase;

Test Yourself: How many fields and how many records will the following query retrieve? a. 7 fields and 14 records b. 14 fields and 7 records c. 7 fields and 9 records d. None of the above SELECT * FROM candy_purchase;

a. 4 b. 8 c. 9 d. 10 e. None of the above Test Yourself: How many CANDY_CUSTOMER records will the following query retrieve? SELECT cust_id FROM candy_customer WHERE cust_name LIKE '%s'

a. 4 b. 8 c. 9 d. 10 e. None of the above Test Yourself: How many CANDY_CUSTOMER records will the following query retrieve? SELECT cust_id FROM candy_customer WHERE cust_name LIKE '%s'

Test Yourself: How many records will the following query retrieve? a. 0 b. 8 c. 14 d. None of the above SELECT * FROM candy_purchase WHERE purch_status LIKE '%Paid%'

Test Yourself: How many records will the following query retrieve? a. 0 b. 8 c. 14 d. None of the above SELECT * FROM candy_purchase WHERE purch_status LIKE '%Paid%'

Test Yourself: How many records will the following query retrieve? a. 0 b. 3 c. 5 d. 8 e. None of the above SELECT * FROM candy_purchase WHERE purch_delivery_date = NULL AND purch_status = 'PAID'

Test Yourself: How many records will the following query retrieve? a. 0 b. 3 c. 5 d. 8 e. None of the above SELECT * FROM candy_purchase WHERE purch_delivery_date = NULL AND purch_status = 'PAID'