COMP102: Introduction to Databases, 4

Similar documents
Chapter 6. SQL Data Manipulation

COMP102: Introduction to Databases, 5 & 6

Lecture 06. Fall 2018 Borough of Manhattan Community College

1) Introduction to SQL

CMP-3440 Database Systems

COMP102: Introduction to Databases, 14

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

CSC Web Programming. Introduction to SQL

COMP102: Introduction to Databases, 9.1

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

CS 582 Database Management Systems II

Step 1: Create and Check ER Model

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

RESTRICTING AND SORTING DATA

STRUCTURED QUERY LANGUAGE (SQL)

30. Structured Query Language (SQL)

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

SQL functions fit into two broad categories: Data definition language Data manipulation language

Data Manipulation Language (DML)

DB Creation with SQL DDL

COMP102: Introduction to Databases, 13

Chapter 3: Introduction to SQL

CMPT 354: Database System I. Lecture 3. SQL Basics

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Lecture 07. Spring 2018 Borough of Manhattan Community College

COMP102: Introduction to Databases, 1 & 2

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

Lecture 2: Chapter Objectives. Relational Data Structure. Relational Data Model. Introduction to Relational Model & Structured Query Language (SQL)

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Database Management Systems,

XQ: An XML Query Language Language Reference Manual

Lecture 2: Chapter Objectives

HNDIT 1105 Database Management Systems

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Relational Database Management Systems for Epidemiologists: SQL Part I

Lecture 6 - More SQL

COMP102: Introduction to Databases, 20

Structure Query Language (SQL)

Deepak Bhinde PGT Comp. Sc.

Exact Numeric Data Types

CS34800 Information Systems. Course Overview Prof. Walid Aref January 8, 2018

Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO

CS317 File and Database Systems

Simple SQL Queries (2)

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

QQ Group

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

12. MS Access Tables, Relationships, and Queries

SQL DATA MANIPULATION. Prepared By: Dr. Vipul Vekariya.

QMF: Query Management Facility

Chapter 3: Introduction to SQL

The SQL data-definition language (DDL) allows defining :

CMP-3440 Database Systems

Full file at

COMP 430 Intro. to Database Systems

Simple queries Set operations Aggregate operators Null values Joins Query Optimization. John Edgar 2

Simple SQL. Peter Y. Wu. Dept of Computer and Information Systems Robert Morris University

Structured Query Language (SQL) Part A. KSE 521 Topic 10 Mun Yi

SQL stands for Structured Query Language. SQL lets you access and manipulate databases

chapter 2 G ETTING I NFORMATION FROM A TABLE

Database Programming with SQL

SQL. Char (30) can store ram, ramji007 or 80- b

SQL Structured Query Language Introduction

Relational Algebra and SQL

618 Index. BIT data type, 108, 109 BIT_LENGTH, 595f BIT VARYING data type, 108 BLOB data type, 108 Boolean data type, 109

Introduction to the Structured Query Language [ SQL ] (Significant Concepts)

Getting Information from a Table

CS2 Current Technologies Lecture 2: SQL Programming Basics

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Chapter-14 SQL COMMANDS

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Queries. Chapter 6. In This Chapter. c SELECT Statement: Its Clauses and Functions. c Join Operator c Correlated Subqueries c Table Expressions

Lesson 2. Data Manipulation Language

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

Oracle Database 11g: SQL and PL/SQL Fundamentals

2) SQL includes a data definition language, a data manipulation language, and SQL/Persistent stored modules. Answer: TRUE Diff: 2 Page Ref: 36

Oracle Database 10g: Introduction to SQL

4. SQL - the Relational Database Language Standard 4.3 Data Manipulation Language (DML)

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

ASSIGNMENT NO 2. Objectives: To understand and demonstrate DDL statements on various SQL objects

Ian Kenny. November 28, 2017

Oracle Database 10g: SQL Fundamentals I


Logical Operators and aggregation

Chapter # 7 Introduction to Structured Query Language (SQL) Part II

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

Chapter 9: Working with MySQL

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

RETRIEVING DATA USING THE SQL SELECT STATEMENT

Access Intermediate

Visual C# 2012 How to Program by Pe ars on Ed uc ati on, Inc. All Ri ght s Re ser ve d.

SQL. SQL Data Manipulation: Queries

Transcription:

COMP102: Introduction to Databases, 4 Dr Muhammad Sulaiman Khan Department of Computer Science University of Liverpool U.K. 7 February, 2011

Introduction: SQL, part 1

Specific topics for today: Purpose and importance of SQL. Retrieving data using SELECT.

Relational Languages There are two main languages: SQL (Structured Query Language): standardized by ISO, most important and most-widely used. QBE (Query-by-Example): alternative graphical point-and-click way of querying database. More intuitive than SQL, and essentiall it converts graphically expressed queries into SQL statements. QBE may not be very effective when applied to more complicated queries that, e.g., involve more tables.

Why SQL? Main language for relational DBMSs; widely used. Based on clean and sound mathematical foundations. Main characteristics: Relatively easy to learn. Non-procedural: you specify what information you require, rather than how to get it. Essentially free-format. Consists of standard English words like SELECT, INSERT, and UPDATE. Can be used by range of users.

Importance of SQL First and, so far, only standard database language to gain widespread acceptance. Huge investment from both vendors and users. Federal Information Processing Standard (FIPS). Used as the basis for other standards.

Objectives of SQL Ideally database language should let user: Create database and table structures. Perform basic tasks like insert, update, delete. Perform both simple and complex queries. Must perform these tasks with minimal user effort. Must be easy to learn. SQL is a transform-oriented language with 2 major components: a DDL for defining database structure; a DML for retrieving and updating data. Until SQL3, SQL did not contain flow of control commands (not computationally complete). SQL can be used interactively or embedded in a high-level language (eg. C, C++). (We focus on interactive SQL.)

Writing SQL commands SQL statement consists of reserved words and user-defined words. Reserved words: fixed part of SQL and must be spelt exactly as required and cannot be split across lines. User-defined words: made up by user and represent names of various database objects such as tables, columns, views. Most components of an SQL statement are case insensitive, except for literal character data. Use extended form of BNF notation: Upper-case letters represent reserved words. Lower-case letters represent user-defined words. indicates a choice among alternatives. Curly braces indicate a required element. Square brackets indicate an optional element.... indicates optional repetition (0 or more).

Two main parts of SQL Data Definition Language (DDL): for defining database structure. SQL commands include here: CREATE TABLE: Creating new tables in the database. CREATE VIEW: Creating new views from the base tables. Data Maniputation Language (DML): for retrieving and updating data. SQL commands include here: SELECT: Retrieving data. INSERT: Inserting data UPDATE: Updating data. DELETE: Deleting data. We will start from the (more complicated) DML part, focusing on SELECT.

Literals Literals are constants used in SQL statements. All non-numeric literals must be enclosed in single quotes (eg. London ). All numeric literals must not be enclosed in quotes (eg. 650.00).

SELECT statement SELECT [DISTINCT ALL] { columnexprn [ AS newname ] [,...] } FROM TableName [ alias ] [,...] [ WHERE condition ] [ GROUP BY columnlist ] [ HAVING condition ] [ ORDER BY columnlist ]

SELECT statement cntd. FROM: Specifies table(s) to be used. WHERE: Filters rows. GROUP BY: Forms groups of rows with same column value. HAVING: Filters groups subject to some condition. SELECT: Specifies which columns are to appear in output. ORDER BY: Specifies the order of the output. Order of the clauses cannot be changed. Only SELECT and FROM are mandatory.

Query 3.1: All columns, all rows Example: List full details of all videos: SELECT catalogno, title, category, dailyrental, price, directorno FROM Video; Can use * as an abbreviation for all columns : SELECT * FROM Video;

Query 3.1: All columns, all rows The result query 3.1 will be the content of table Video itself:

Query 3.2: Specific Columns, All Rows List the catalog number, title and daily rental rate of all videos: SELECT catalogno, title, dailyrental FROM Video;

Query 3.3: Use of DISTINCT (a) List all video categories: SELECT category FROM Video; (b) Use DISTINCT to eliminate duplicates: SELECT DISTINCT category FROM Video;

Query 3.4: Calculated Fields List rate for renting videos for 3 days: SELECT catalogno, title, dailyrental * 3 FROM Video; Other arithmetic operators: +,,, parantheses. To name column, use AS clause: SELECT catalogno, title, dailyrental * 3 AS threedayrate FROM Video;

Query 3.5: Comparison search condition List all staff with a salary greater than $10,000: SELECT staffno, name, position, salary FROM Staff WHERE salary > 10000;

Query 3.6: Range Search Condition List all staff with a salary between $ 45,000 and $ 50,000: SELECT staffno, name, position, salary FROM Staff WHERE salary BETWEEN 45000 AND 50000; Note: BETWEEN test includes the endpoints of range.

Query 3.6: Range Search Condition cntd. Also a negated version NOT BETWEEN. BETWEEN does not add much to SQL s expressive power. Could also write: SELECT staffno, name, position, salary FROM Staff WHERE salary >= 45000 AND salary <= 50000; Useful, though, for a range of values. Comparison operators in conditions: =, <, >, <>, <=, >=.

Query 3.7: Set Membership List all videos in the Action and Children categories: SELECT catalogno, title, category FROM Video WHERE category IN ( Action, Children );

Query 3.7: Set Membership cntd. There is a negated version (NOT IN). IN does not add much to SQL s expressive power. Could have expressed this as: SELECT catalogno, title, category FROM Video WHERE category = Action OR category = Children ; IN is more efficient when set contains many values.

About logical predicates (conditions) More complex conditions can be build using logical operators: AND, OR, NOT, with parantheses if needed. The rules for evaluating predicates: An expression is evaluated from left to right. Subexpressions in parantheses are evaluated first. NOTs are evaluated before ANDs and ORs. ANDs are evaluated before ORs.

Query 3.8: Pattern Matching List all staff whose first name is Sally: SELECT staffno, name, position, salary FROM Staff WHERE name LIKE Sally% ; SQL has two special pattern matching symbols: %: sequence of zero or more characters; (underscore): any single character. LIKE Sally% : first 5 characters must be Sally followed by anything.

More about strings Comparing strings: Two strings a 1 a 2 a n, b 1 b 2 b m, can be compared (lexicographically) using operators: =, <, >, >=, <=. a 1 a 2 a n < b 1 b 2 b m if a 1 < b 1, or a 1 = b 1 and a 2 < b 2, or a 1 = b 1 and a 2 = b 2 and a 3 < b 3, and so on. Also a 1 a 2 a n < b 1 b 2 b m if n < m and a 1 a 2 a n = b 1 b 2 b n. Example: fodder < foo, fod < foremost, bar < bargain. To search for strings containing pattern-matching character, use escape character: Say for 15%, use: LIKE 15#% ESCAPE #. For any string that begins and ends with %, use: LIKE x%%x% ESCAPE x.

Query 3.9: NULL Search Condition List the video rentals that have not yet been returned: Have to test for null explicitly using special keyword IS NULL: SELECT dateout, memberno, videono FROM RentalAgreement WHERE datereturn IS NULL; Negated version (IS NOT NULL) can test for non-null values.

Query 3.10: Single Column Ordering List all videos in descending order of price: SELECT * FROM Video ORDER BY price DESC; Increasing (ascending) order: ASC.

Multiple-Column Ordering List all videos in descending order of price, and in case two or more prices are same, order in ascending order of title: SELECT * FROM Video ORDER BY price DESC, title ASC;