SQL Exercises – Part I April-26-17.

Slides:



Advertisements
Similar presentations
SQL Query Examples Database Management COP4540, SCS, FIU.
Advertisements

16.3 Parser to Logical Query Plans SQL(not RAE) Figure 16.2 select distinct movietitle from starsIn where starname in (select name from moviestar.
Oracle Labs ECS 242, 342, 360 –You can connect from home to the machines in the lab. –E.g.: ssh u-knoppix.csc.uvic.ca Execute “sh” to use the proper shell.
SQL This presentation will cover: A Brief History of DBMS View in database MySQL installation.
Database Modifications CIS 4301 Lecture Notes Lecture /30/2006.
SQL reviews. Stored Procedures Create Procedure Triggers.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
Subqueries Example Find the name of the producer of ‘Star Wars’.
Indexes. An index on an attribute A of a relation is a data structure that makes it efficient to find those tuples that have a fixed value for attribute.
Instructor: Amol Deshpande  Data Models ◦ Conceptual representation of the data  Data Retrieval ◦ How to ask questions of the database.
Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin.
CMSC424: Database Design Instructor: Amol Deshpande
SQL. 1.SQL is a high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
CMSC424: Database Design Instructor: Amol Deshpande
CSE 190: Internet E-Commerce Lecture 10: Data Tier.
Movies length titleyearfilmType Voices isa Cartoons isa MurderMystery weapon toStar Our Movie Example.
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 7.
CMSC424: Database Design Instructor: Amol Deshpande
SQL SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
Database Modifications A modification command does not return a result as a query does, but it changes the database in some way. There are three kinds.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
CMSC424: Database Design Instructor: Amol Deshpande
CMSC424: Database Design Instructor: Amol Deshpande
SQL By: Toan Nguyen. Download Download the software at During the installation –Skip sign up for fast installation.
1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.
Chapter 6 The database Language SQL Spring 2011 Instructor: Hassan Khosravi.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1.
Relational Algebra CIS 4301 Lecture Notes Lecture /28/2006.
SQL 2014, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web.
Relational Algebra Spring 2012 Instructor: Hassan Khosravi.
Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture /03/2006.
Introduction to Indexes. Indexes An index on an attribute A of a relation is a data structure that makes it efficient to find those tuples that have a.
CPS216: Advanced Database Systems Query Rewrite Rules for Subqueries Shivnath Babu.
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
THE DATABASE LANGUAGE SQL
Index Example From Garcia-Molina, Ullman, and Widom: Database Systems, the Complete Book pp
1 Chapter 6 Constraints uForeign Keys uConstraints.
1 More SQL uDatabase Modification uDefining a Database Schema uViews.
Referential Integrity checks, Triggers and Assertions Examples from Chapter 7 of Database Systems: the Complete Book Garcia-Molina, Ullman, & Widom.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
© D. Wong Normalization  Purpose: process to eliminate redundancy in relations due to functional or multi-valued dependencies.  Decompose relation.
CS 157B Database Systems Dr. T Y Lin. Updates 1.Red color denotes updated data (ppt) 2.Class participation will be part of “extra” credits to to “quiz.
Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 2 THE RELATIONAL MODEL OF DATA 1.
1 Chapter 6 More SQL uDatabase Modification uDefining a Database Schema uViews.
The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems.
The Database Language SQL Prof. Yin-Fu Huang CSIE, NYUST Chapter 6.
CS 157B Database Systems Dr. T Y Lin. 1.2 Overview of a Database Management System Data-Definition Language Commands –Illustrated by three examples.
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
1 SQL: Concept and Usage. 2 SQL: an Overview SQL (Structured Query Language) –Also be pronounced as “sequel” –A relational database language –Consists.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
Chap 5. The DB Language (SQL)
16.2.Algebraic Laws for Improving Query Plans
THE DATABASE LANGUAGE SQL
Introduction to Structured Query Language (SQL)
THE RELATIONAL MODEL OF DATA
Chap 2. The Relational Model of Data
THE RELATIONAL MODEL OF DATA
SQL: Concept and Usage.
2018, Fall Pusan National University Ki-Joune Li
16.2.Algebraic Laws for Improving Query Plans
SQL This presentation will cover: View in database MySQL installation
Deletion in AVL Tree There are 5 cases to consider.
CS4433 Database Systems SQL - Basics.
Query Compiler By:Payal Gupta Shirali Choksi Professor :Tsau Young Lin.
SQL (Structured Query Language)
Presentation transcript:

SQL Exercises – Part I April-26-17

Some Simple SQL Commands CREATE TABLE test ( i int,    s char(10)      ); DESCRIBE test;

Inserting tuples into table INSERT INTO test VALUES(10, 'foobar1'); INSERT INTO test VALUES(11, 'foobar2'); INSERT INTO test VALUES(12, 'foobar3');

Querying the table SELECT * FROM test; SELECT * FROM test where i=10; SELECT * FROM test where s=‘foo’; SELECT * FROM test where s like ‘fo%’;

Deleting tuples from table Delete from test where i=10; Delete from test; DROP TABLE test;

Exercise #1 MovieExec (name, address, cert#, netWorth) Studio (name, address, precsC#) Describe the tuples that would appear in the following expression: Studio CROSS JOIN MovieExec

Solution #1 - 1 Q: Studio CROSS JOIN MovieExec A: The result will be a 7-column relation with all the attributes of Studio and MovieExec. Every pair consisting of one tuple of Studio and one tuple of MovieExec will be a tuple of the resulting relation

Solution #1 - 2 Instance Studio: Instance MovieExec: 200 100 netWorth 1 H G 2 F E cert# address name name address presC# A B 1 C D 2 200 100 netWorth 1 2 cert# H G D C F MovieExec.addree E MovieExec.name B A presC# Studio.address Studio.name Studio x MovieExec:

Exercise #2 - 1 Movie(title, year,length, inColor) StarsIn(movieTitle, movieYear, starName) MovieStar(name, address, gender, birthdate, income) Q-1: Find Harrison Ford’s birth date.

Solution #2 - 1 Q-1: Find Harrison Ford’s birth date. Select birthdate From MovieStar Where name=‘Harrison Ford’

Exercise #2 - 2 Movie(title, year,length, inColor) StarsIn(movieTitle, movieYear, starName) MovieStar(name, address, gender, birthdate, income) Q-2: Find all the movie stars who earn at least $10,000,000

Solution #2 - 2 Q-2: Find all the movie stars who earn at least $10,000,000 Select name From MovieStar Where income>= 10,000,000

Exercise #2 - 3 Movie(title, year,length, inColor) StarsIn(movieTitle, movieYear, starName) MovieStar(name, address, gender, birthdate, income) Q-3: Find all the stars who either are male or live in Montreal (have string Montreal as part of their address)

Solution #2 - 3 Q-3: Find all the stars who either are male or live in Montreal (i.e. have string Montreal as part of their address) Select name From MovieStar Where gender=‘M’ or address like ‘%Montreal%’

Exercise #2 - 4 Movie(title, year,length, inColor) StarsIn(movieTitle, movieYear, starName) MovieStar(name, address, gender, birthdate, income) Q-4: Find all the color movies (i.e. inColor=‘color’) that were made in 1980 and have length more than 80 minutes.

Solution #2 - 4 Q-4: Find all the color movies (i.e. inColor=“color”) that were made in 1980 and have length more than 80 minutes. Select title From Movie Where inColor=‘color’ and year=1980 and length>80

Exercise #2 - 5 Movie(title, year,length, inColor) StarsIn(movieTitle, movieYear, starName) MovieStar(name, address, gender, birthdate, income) Q-5: Find all the color movies that Harrison Ford has played.

Solution #2 - 5 Q-5: Find all the color movies that Harrison Ford has played Select title From Movie, StarsIn Where title=movieTitle and year=movieYear and starName =‘Harrison Ford’ and inColor =‘color’

Exercise #3 Create an Employee table that can be used to store information related to employee’s first name, last name, SIN, employee number, birthdate, address, gender, and salary.

Solution #3 CREATE TABLE EMPLOYEE (FirstName VARCHAR(15) NOT NULL, LastName VARCHAR(15) NOT NULL, SIN CHAR(9) NOT NULL, EmployeeNum CHAR(12) NOT NULL, BirthDate DATE, Address VARCHAR(30), Gender CHAR, Salary DECIMAL(10,2), PRIMARY KEY (SIN), UNIQUE (EmployeeNum ));