SQL grouping, views & modifying data

Similar documents
SQL DDL. Intro SQL CREATE TABLE ALTER TABLE Data types Service-based database in Visual Studio Database in PHPMyAdmin

Chapter 6. SQL: SubQueries

Objective. The goal is to review material covered in Chapters 1-5. Do the following questions from the book.

1 P a g e D a t a b a s e o l d p a p e r b y w a s i m

HKTA TANG HIN MEMORIAL SECONDARY SCHOOL SECONDARY 3 COMPUTER LITERACY. Name: ( ) Class: Date: Databases and Microsoft Access

CSC Web Programming. Introduction to SQL

6.9 List the names and addresses of all guests in London, alphabetically ordered by name.

SQL-Server. Insert query in SQL Server. In SQL Server (Transact-SQL), the INSERT statement is used to

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

Lecture 03. Spring 2018 Borough of Manhattan Community College

Lecture 07. Spring 2018 Borough of Manhattan Community College

Relational Database Development


Lesson 2. Data Manipulation Language

CMP-3440 Database Systems

CLOUDPM GUEST PROFILES USER GUIDE

Topic of most interest

Chapter 8: Working With Databases & Tables

Data Manipulation Language (DML)

T-SQL Training: T-SQL for SQL Server for Developers

tablename ORDER BY column ASC tablename ORDER BY column DESC sortingorder, } The WHERE and ORDER BY clauses can be combined in one

CS317 File and Database Systems

Rows and Range, Preceding and Following

Database Management Systems (Classroom Practice Booklet Solutions)

SECTION 1 DBMS LAB 1.0 INTRODUCTION 1.1 OBJECTIVES 1.2 INTRODUCTION TO MS-ACCESS. Structure Page No.

Lecture 03. Fall 2017 Borough of Manhattan Community College

SQL stands for Structured Query Language. SQL is the lingua franca

Assorted Topics Stored Procedures and Triggers Pg 1

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

Introduction to Databases, Fall 2005 IT University of Copenhagen. Lecture 2: Relations and SQL. September 5, Lecturer: Rasmus Pagh

THE COPPERBELT UNIVERSITY

Introduction to Database Systems CSE 414

Auto Night Audit Reports Configuring Auto Night Audit Reporting Group Folio Summary Charges Tax Invoice View:...

CS1100: Data, Databases, and Queries QUERY CONSTRUCTION. CS1100 Microsoft Access 1

Server-side Web Programming

Database Management Systems,

3ISY402 DATABASE SYSTEMS

MTAT Introduction to Databases

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

Access to the Online Booking Tool

Relational Data Structure and Concepts. Structured Query Language (Part 1) The Entity Integrity Rules. Relational Data Structure and Concepts

EECS 647: Introduction to Database Systems

QUICK REFERENCE GUIDE. Version

Querying Data with Transact-SQL (20761)

Activant Solutions Inc. SQL 2005: Basic Data Manipulation

1Z0-071 Exam Questions Demo Oracle. Exam Questions 1Z Oracle Database 12c SQL.

FIT 100 More Microsoft Access and Relational Databases Creating Views with SQL

How to Guide: Using the OTA Extranets

GUJARAT TECHNOLOGICAL UNIVERSITY

SQL Simple Queries. Chapter 3.1 V3.01. Napier University

1Z0-071 Exam Questions Demo Oracle. Exam Questions 1Z Oracle Database 12c SQL.

Querying Data with Transact SQL

Microsoft Querying Microsoft SQL Server 2014

SQL Server Administration Class 4 of 4. Activant Prophet 21. Basic Data Manipulation

Structured Query Language. ALTERing and SELECTing

Chapter 8 Relational Tables in Microsoft Access

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

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

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

Intro to Structured Query Language Part I

Querying Microsoft SQL Server

Introduction to Data Management CSE 344

CSE 530A. Inheritance and Partitioning. Washington University Fall 2013

Structured Query Language (SQL) lab syllabus 4 th science. SQL is used to communicate with a database. it is the standard language for relational

QUERYING MICROSOFT SQL SERVER COURSE OUTLINE. Course: 20461C; Duration: 5 Days; Instructor-led

Querying Data with Transact-SQL

THE CAR SHARE RESERVATION DATABASE

Networks and Web for Health Informatics (HINF 6220)

WEEK 3 TERADATA EXERCISES GUIDE

Queries with Multiple Criteria (OR)

The Relational Model

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

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName

SQL - Subqueries and. Schema. Chapter 3.4 V4.0. Napier University

Database Programming with SQL

Databases II: Microsoft Access

Announcements. Outline UNIQUE. (Inner) joins. (Inner) Joins. Database Systems CSE 414. WQ1 is posted to gradebook double check scores

The attendee will get a deep dive into all the DDL changes needed in order to exploit DB2 V10 Temporal tables as well as the limitations.

EE221 Databases Practicals Manual

COUNT Function. The COUNT function returns the number of rows in a query.

Structured Query Language Continued. Rose-Hulman Institute of Technology Curt Clifton

DATABASE MANAGEMENT SYSTEMS PREPARED BY: ENGR. MOBEEN NAZAR

Sankalchand Patel College of Engineering, Visnagar B.E. Semester III (CE/IT) Database Management System Question Bank / Assignment

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

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

SQL Aggregate Functions

Relational Model. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

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

Appendix A. Using DML to Modify Data. Contents: Lesson 1: Adding Data to Tables A-3. Lesson 2: Modifying and Removing Data A-8

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

Business Partner Master Data Practice Lab

CSE 344 JANUARY 5 TH INTRO TO THE RELATIONAL DATABASE

Relational Database Management Systems for Epidemiologists: SQL Part II

Create and Modify Queries 7

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus

INFSCI 2710 Database Management Solution to Final Exam

Course 20461C: Querying Microsoft SQL Server

Relational Database Management Systems for Epidemiologists: SQL Part I

Transcription:

SQL grouping, views & modifying data Grouping (GROUP BY) Views INSERT UPDATE DELETE Steen Jensen, autumn 2017

SQL query - grouping Grouping makes it possible to show information listed in groups Conditions can be made by using the HAVING clause not mandatory Syntax: SELECT <fieldlist> FROM <tablename> GROUP BY <fieldlist> HAVING <criteria> SELECT count(*), types, sum(price) FROM room GROUP BY types HAVING sum(price)>3000 ; 2

SQL query - grouping But wait a minute isn t HAVING the same as WHERE..? Not quite WHERE filters specific records HAVING filters specific groups from the final result You can t use an aggregate function in a WHERE clause 3

Exercise grouping With the data in place for the hotel database then run the below queries: What is the (average) number of bookings for each hotel in this month? (February in 2011) What is the lost income from unoccupied rooms at each hotel this month? As This month pick a month represented in the SQL table Booking. 4

CREATE VIEW intro A view is defined as a query on one or more base tables or views To the database user, a view appears just like a real table with columns & rows However, unlike a base table, a view is not necessarily stored in the database The DBMS stores the definition of the view in the database The subselect is known as the defining query 5

CREATE VIEW example 1 6

CREATE VIEW example 2 7

CREATE VIEW example 3 8

DROP VIEW A view is removed from the database with the DROP VIEW 9

Advantages and disadvantages of views 10

Explanation of selected advantages 11

Explanation of selected disadvantages 12

Exercise in CREATE VIEW Continue with the Hotel case Create a view containing the hotel name of hotels in XXX (replace XXX with a specific city from your Hotel table) Create a view containing the hotel name and the names of the guests staying at each hotel Create a view containing the hotel name and a count of the number of guests staying at each hotel Create a view containing the hotel name and the room type for each hotel the different room types at each hotel should only be shown once (avoid duplicates) 13

Insert a row in a table: INSERT INSERT INTO <tablename> VALUES (<valuelist>) Example: INSERT INTO hotel VALUES (1,'The Pope','Vaticanstreet 1 1111 Bishopcity'); NB! The first value (1) in VALUES is NOT necessary, as the field/column Hotel_No is assigned by the DBMS

INSERT Things to note about INSERT The value list must match the field list for the table into which the record is inserted If we try to insert a record with a key field which already exists, we will get an error Null values can be inserted if the table definition allows it The field list can be specified explicitly INSERT INTO hotel (Hotel_No, Name, Address) VALUES (1,'The Pope','Vaticanstreet 1 1111 Bishopcity');

UPDATE Updates values for specified field(s) Without a WHERE: all rows/records UPDATE <tablename> SET field1 = value1, field2 = value2, WHERE <condition>

UPDATE Example: update hotel set name ='The Great Pope' where hotel_no= 1;

UPDATE Things to note about UPDATE For each field update, the type of the value must match the type of the field The WHERE clause is optional if you leave it out, all records in the table are updated! It is not considered an error if zero rows are changed, so pay attention to the condition in the WHERE clause

DELETE SQL syntax DELETE FROM table_name WHERE some_column=some_value Delete all rows / records from GUEST : DELETE FROM Guest; Delete all rooms from hotel no 1: DELETE FROM Room where Hotel_no = 1;

Exercise in modifying data With the data in place, run the below commands on the database INSERT INTO Hotel VALUES ( specify your own values) INSERT INTO Room VALUES (specify your own values) UPDATE Room SET Price = Price* 1.30; DELETE FROM Room WHERE (Room_no = 8) Now formulate commands yourself, in order to: Insert data about Scandic Roskilde in the table Hotel (you can find the data on the Internet, or make it up yourself) Insert data representing the fact that Hotel Scandic have 10 rooms with room numbers 101, 102, 103, 201, 202, 203, 301, 302, 303, 400 Update the name of the Hotel Scandic Roskilde to The new Scandic Roskilde Insert data for a booking of a room at the hotel The new Scandic Roskilde