MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 4 © Akhilesh Bajaj, 2000, 2002, 2004, 2008. All.

Slides:



Advertisements
Similar presentations
© Abdou Illia MIS Spring 2014
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Database Design -- Basic SQL
1 Lecture 11: Basic SQL, Integrity constraints
E-R Diagram for a Banking Enterprise
Information Resources Management February 27, 2001.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
1 Query Languages: How to build or interrogate a relational database Structured Query Language (SQL)
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
1 Table Alteration. 2 Altering Tables Table definition can be altered after its creation –Adding columns –Changing columns’ definition –Dropping columns.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
SQL Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
SQL: Interactive Queries (1) John Ortiz Lecture 11SQL: Interactive Queries (1)2 Basic Select Statement  Basic form of the select statement: select target-attribute-list.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 5B Introduction to SQL.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:
Lecture 05 Structured Query Language. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 1 ©Akhilesh Bajaj, 2000, 2002, 2003, All.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 5 © Akhilesh Bajaj, 2000, 2002, 2003, All.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Structured Query Language
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, All Rights.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
Lectures 2&3: Introduction to SQL. Lecture 2: SQL Part I Lecture 2.
MIS3053 Database Design And Applications The University Of Tulsa Professor: Akhilesh Bajaj Normal Forms Lecture 2 © Akhilesh Bajaj, 2000, 2002, 2003,2004.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
1 SQL Chapter 9 – 8 th edition With help from Chapter 2 – 10 th edition.
Writing Basic SQL SELECT Statements Lecture
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
 CONACT UC:  Magnific training   
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Introduction to Database Programming with Python Gary Stewart
SQL and Relational Algebra Edel Sherratt Nigel Hardy Horst Holstein.
Structured Query Language (SQL) DML
Chapter 3 Introduction to SQL
Chapter 3 Introduction to SQL(2)
Database Systems SQL cont. Relational algebra
Query-by-Example (QBE)
Database Systems: Design, Implementation, and Management Tenth Edition
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
Writing Basic SQL SELECT Statements
CS 405G: Introduction to Database Systems
CS 405G: Introduction to Database Systems
CMPT 354: Database System I
Writing Basic SQL SELECT Statements
Lectures 3: Introduction to SQL 2
Database Systems: Design, Implementation, and Management Tenth Edition
Presentation transcript:

MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 4 © Akhilesh Bajaj, 2000, 2002, 2004, All Rights Reserved.

Goals for Today Learn the basics of the query component of SQL - the SELECT clause - the WHERE clause - the FROM clause - the AS clause - the LIKE operator - the ORDER BY clause - the UNION operation - the INTERCEPT operation - the MINUS operation

Structured Query Language (SQL) The standard relational database language Used to query the database (hardest to learn!), to define the relational schema, and to insert, delete and update data in the database. Two standards exist: SQL-89 and SQL-92. Most systems support at least SQL-89. SQL is the standard language with which you interact with the relational database system. SQL was invented in the early 1970-s. It has been around for around 25 years, and likely to be around for decades ahead.

Structured Query Language (SQL): Query Part To use SQL for querying, we need a defined relational schema to already exist. We also assume there is some data in the tables. SQL allows the use of null values. The basic query clause in SQL is the select from where Condition ; clause. Note that the select here is similar to the project in relational algebra. The where here is similar to the select in relational algebra.

Relational Schema For Example SQL Queries Professors (f_id, f_name, f_address, f_specialty, highest_degree) Courses (c_id, c_name, num_credits) Sections (c_id, s_id, time_held, week_days_held, date_began, date_ended, cl_id) Students (st_id, st_name, gender, st_gpa) Grades (st_id, c_id, s_id, grade) Classrooms (cl_id, capacity, location) Teach (f_id, c_id, s_id)

Structured Query Language (SQL): Query Part Example: Information on all Professors who have a Ph.D. degree and specialize in “Databases”.

Structured Query Language (SQL): Query Part The SELECT clause: The result of an SQL query is a relation (or table). This result table is NOT stored as part of the schema; instead it is either printed on the screen or stored as a file. Note that in SQL the result table permits duplicates by default. This is unlike relational algebra. The SELECT DISTINCT clause forces elimination of duplication. The SELECT ALL clause forces duplication. The * symbol after SELECT means all attributes. Note: * is also used as the “multiply” operator The SELECT clause can also contain arithmetic expressions E.g., SELECT st_gpa*10 FROM students; /*This query gives the GPAs multiplied by 10*/

Structured Query Language (SQL): Query Part The WHERE clause: The WHERE clause specifies the conditions that will select all the rows we want. Conditions are formed using >, =,<>, AND, OR, NOT, BETWEEN AND and NOT BETWEEN AND. E.g., SELECT st_name FROM students WHERE st_gpa BETWEEN 2.0 AND 3.0;

Structured Query Language (SQL): Query Part The FROM clause: The FROM clause defines a cartesian product of all the tables in the clause. This means that the “garbage” rows have to be explicitly removed using the WHERE clause. This is VERY IMPORTANT. E.g., SELECT f_name FROM professors, teach WHERE professors.f_id = teach.f_id AND teach.c_id = ‘MIS3053’;

Structured Query Language (SQL): Query Part The AS clause: The AS clause is used to rename both tables and attributes. Tables are renamed in the FROM clause, attributes in the SELECT clause. E.g., SELECT st_name AS names FROM students WHERE st_gpa > 3.5; SELECT S.st_name AS names FROM students AS T, students AS S WHERE T.st_name = ‘Jill Jones’ AND S.st_gpa > T.st_gpa; Result: names ______ Bob Smith Al Stewart NB: In Oracle, the AS clause for tables is a blank.

Structured Query Language (SQL): Query Part The LIKE operator: The LIKE operator is used for string matching. The % character matches any substring. In MS Jet SQL/ Access it is *. The _ (underscore) character matches any single character. E.g., SELECT f_name AS Prof_names FROM professors WHERE f_name LIKE ‘%aj%’;

Structured Query Language (SQL): Query Part The ORDER BY clause: The ORDER BY clause orders the rows returned as the result. E.g., SELECT st_name FROM students WHERE st_gpa > 3.0 ORDER BY st_gpa ASC; SELECT st_name FROM students WHERE st_gpa > 3.0 ORDER BY st_gpa ASC, st_id ASC; ORDER BY causes the query to run slowly, since sorting takes up time.

Structured Query Language (SQL): Query Part The UNION operation: The UNION operation works identically to that in relational algebra. E.g., (SELECT st_name as all_names FROM students) UNION (SELECT f_name AS all_names FROM professors); The UNION operator automatically eliminates duplicates (unlike the SELECT clause). In other words, duplicates are not the default for union. To enforce duplication, use the UNION ALL operator.

Structured Query Language (SQL): Query Part The INTERSECT operation: The INTERSECT operation works identically to that in relational algebra. E.g., (SELECT st_name AS all_names FROM students) INTERSECT (SELECT f_name AS all_names FROM professors);

Structured Query Language (SQL): Query Part The MINUS operation: The MINUS operation works identically to that in relational algebra. E.g., (SELECT st_name FROM students) MINUS (SELECT st_name FROM students AS S, grades AS T WHERE S.st_id = T.st_id AND T.c_id = ‘MIS3053’ AND T.s_id = ‘ASpr2006’); NB: MINUS is not supported on all systems.

Structured Query Language (SQL): Query Part Some Practice Examples 1. The names an student ids of all students who have an ‘A’ in MIS3053AFall The names of all Professors who have never given a ‘C’. 3. The names of all Professors who teach ‘Johnny Jones’.