NULLs & Outer Joins. Objectives of the Lecture :

Size: px
Start display at page:

Download "NULLs & Outer Joins. Objectives of the Lecture :"

Transcription

1 Slide 1 NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.

2 Slide 2 Missing Values : Possible Strategies Use a special value to represent missing data. E.g. N/A, T.B.A. The special value must have the same data type as the data that is missing, so it can be stored with the data that is known. Requires no special facility from the DBMS. Use NULL to represent missing data. NULL is the absence of a value. space NULL 0 NULL character NULL is not part of any data type. Requires special support from the DBMS. SQL DBMSs provide this support. So most DBs use NULLs to represent missing data. This is revision based on part of the earlier lecture The Data in a Relation. Slide 3 Display of SQL NULLs EmpNo EName M-S Sal E3 Smith S 18,000 E5 Robinson M 24,000 E9 Graham S E1 Robson D 32,500 E2 Atkins 24,000 E6 Blakelaw M 54,000 E7 Mortimer D 28,000 E4 Fenwick S 40,000 Blank space in Oracle. Keyword NULL in other SQL DBMSs. Other possibilities in other DBMSs. This is how Oracle displays NULLs in a retrieved table. Other SQL DBMSs use different conventions to display a NULL.

3 Slide 4 Dealing with NULLs in SQL Tables Three situations arise : Comparisons of column values. This occurs in the SQL equivalents of the Restrict and the various Join operations, plus Deletions and Updates. Calculations involving column values. This occurs in the SQL equivalents of the GroupBy and Extend operations. Comparisons of row values. This occurs in the SQL equivalents of the Project, GroupBy, Union, Intersect, and Difference operations. Each of these three cases is now considered in turn :-

4 Case One : Comparison of Column Values Slide 5 Comparison of Column Values (1) SQL provides special comparators to check for NULL :- X IS NULL X IS NOT NULL Let X be a numeric column. If X has a value, the comparison X = 3 makes sense. It should yield true or false. Suppose X is NULL. An error should arise. In fact SQL treats the NULL as representing an existing but unknown value. Comparison returns maybe. Rationale : We don t know if X = 3 because X is NULL (= not available). Note : X may represent some other case of missing data (e.g. not applicable, does not exist). The result is still maybe even though this is then illogical. NULLs can be used to represent many different cases of missing data. Each different case may require its own rationale for how to handle missing data, and they can vary significantly. So SQL s choice of rationale will generally only be valid in certain cases.

5 Slide 6 Comparisons of Column Values (2) Let X and Y be a numeric columns. Consider the comparison X = Y Suppose X and Y are both NULL. The result is maybe not true. NULL is not the same as maybe. Absence of a value. A truth value. SQL uses NULL to mean maybe! To avoid confusion in the remainder of the lecture, we will still assume the value maybe exists in SQL.

6 Slide 7 Restricts, Joins, Updates and Deletions Restrict SELECT * FROM TableName WHERE condition ; Join SELECT * FROM Table1 NATURAL JOIN Table2 ; Delete Update DELETE FROM TableName WHERE condition ; UPDATE TableName SET column(s) = new value(s) WHERE condition ; Column comparison used as a condition Similarly for other kinds of Join. Restrict / Join / Delete / Update action taken only where condition evaluates to true, not where it evaluates to maybe or false. In principle, there is no problem with this. In practice, problems arise because the presence of NULLs is forgotten, or it is assumed that SQL will take the same reasonable approach to NULLs that the user does, when in fact SQL doesn t take that approach. We need to assume NULLs may occur and include appropriate conditions for them. Consider an example of what happens if this is forgotten :-

7 Slide 8 Unexpected Results (1) They arise when forgetting that the condition can evaluate to maybe. Example :- SELECT * FROM EMP WHERE Sal >= UNION SELECT* FROM EMP WHERE Sal < ; If column Sal contains any NULLs, the result will not re-create table EMP. the 2 Restrictions will not necessarily contain all the rows of EMP between them. Of course it is not assumed that you will use 2 queries simply to re-create the original table! Each Restriction condition is the logical inverse of the other. Consequently there is a temptation to think that those rows not retrieved from the table by one query must inevitably be retrieved by the other query. This is guaranteed to be true if it were not for NULLs. If there are NULLs in the table, there will be a third set of rows only retrieved from the table by a Restriction condition that retrieves those rows containing NULLs. Therefore it is essential to decide when formulating the required Restriction condition whether the NULL-including rows should be retrieved as well. The following shows the Restriction conditions that correspond to the three sets of rows that arise when NULLs are included.

8 Slide 9 Unexpected Results (2) To ensure the table is re-created, re-write the query as follows :- In general, adjust statements to reflect the NULL possibility. SELECT * FROM EMP WHERE Sal >= UNION SELECT * FROM EMP WHERE Sal < UNION SELECT * FROM EMP WHERE Sal IS NULL ; In this particular case, should the rows with NULL Sals be retrieved with the salaries that are 20,000 or more, or with those that are less than 20,000; or do we genuinely want to ignore the NULL Sals?

9 Slide 10 Join involving NULLs : Example P# S# Qty Details P1 S1 5.. P2 S2 7.. R P# S# Qty P1 S1 5 P2 10 P2 S2 7 S S# Details S 1.. S 2.. S 3.. This row does not appear in the result. SELECT * FROM R Natural Join S ; If we want to include the missing row, we need to replace the R Natural Join S with R Join S Using On( some condition involving NULLs )

10 Slide 11 Oracle s NVL Function NVL supplies a value to use whenever a NULL is encountered. It can be used in SELECT and WHERE phrases. Example : NVL( Sal, 0 ) Put a column name in the first position. Put a value in the second position. This yields the value of the Sal column, except that if Sal is NULL, then a value of zero is returned. NVL can be used to ensure a comparison always yields true or false, and never maybe. Value S is used in the comparison when M-S is NULL. Comparison can never return maybe. Example :... WHERE NVL( M-S, S ) <> M Most SQL DBMSs have some function analogous to Oracle s NVL, although it may be somewhat different. Such a function can be very useful in practice, and can make it easier to design conditions that cope with possible NULLs.

11 Case Two : Calculations Involving Column Values Slide 12 Calculations Involving Column Values These arise in two situations : Scalar calculations along rows Extend EmpNo EName M-S Sal Aggregate calculations along columns GroupBy E3 Smith S E5 Robinson M 24,000 E9 Graham S E1 Robson D 32,500 E2 Atkins M E6 Blakelaw M E7 Mortimer D 28,000 E4 Fenwick S 40,000 We will now consider these two situations :-

12 Slide 13 Scalar Calculations Any calculation involving a NULL results in a NULL. Examples : let n be NULL. Then :- n + 1 NULL n concatenate ABCD NULL n - n NULL (not zero) Example :- SELECT Sal AS NewSal FROM EMP ; So NewSal will be NULL whenever Sal is NULL. This can give some surprising results. Take care! Slide 14 Aggregate Calculations If the columns being aggregated contain one or more NULLs, then the answer from : Sum Avg Min ignores the NULLs. Max Count( Distinct ) Count(*) includes the NULLs. Not all of these are mathematically valid. Take note and take care!

13 Slide 15 Example : Aggregation in GroupBy EmpNo EName M-S Sal Total M-S 40,000 S 24,000 M 60,500 D E3 Smith S E5 Robinson M 24,000 E9 Graham S E1 Robson D 32,500 E2 Atkins M E6 Blakelaw M E7 Mortimer D 28,000 E4 Fenwick S 40,000 SELECT Sum(Sal) AS Total, M-S FROM EMP GROUP BY M-S ; We consider here the aggregate calculation aspects of GroupBy, not the grouping aspects. Depending on the circumstances, what SQL does about NULLs may or may not be appropriate. See the earlier discussion on the rationale concerning missing data.

14 Case Three : Comparison of Row Values Slide 16 Comparisons of Rows In SQL, two rows are identical if : they have the same number of attributes; corresponding attributes are of the same data type; corresponding attributes have the same value. In an SQL row comparison, a NULL compared to a NULL true In an SQL column comparison (e.g. for a Join operation) a NULL compared to a NULL maybe Different!!

15 Slide 17 Example : Row Comparison E_No E_Name M_S Sal E2 Atkins 24,000 E2 Atkins 24,000 Comparison of M-S column values : Row Comparison : 2 NULLs are defined to be identical. A comparison yields true!! these 2 rows are identical. Column Comparison : 2 NULLs are not assumed identical. A comparison yields maybe!! these rows are rejected. Slide 18 Project, GroupBy, & Set Operators Project SELECT DISTINCT ColumnName(s) FROM TableName ; GroupBy SELECT Aggregation(s), GroupingCol(s) FROM TableName GROUP BY GroupingCol(s) ; Set Ops SELECT * FROM TableName1 UNION SELECT * FROM TableName2 ; Similarly for the other Set Ops, Intersect & Except/Minus. Project / GroupBy (grouping rows) / Union / Intersect / Except action taken on the basis that all NULLs are identical. Consider some examples :-

16 Slide 19 Example : Projection SELECT DISTINCT M-S FROM EMP ; EmpNo EName M-S Sal M-S S M D W E3 Smith S 18,000 E5 Robinson M 24,000 E9 Graham 18,000 E1 Robson D 32,500 E2 Atkins 24,000 E6 Blakelaw M 54,000 E7 Mortimer D 28,000 E4 Fenwick W 40,000 Slide 20 Example : Grouping in GroupBy SELECT Aggregation, M-S FROM EMP GROUP BY M-S ; EmpNo EName M-S Sal Aggregate Agg-Val1 Agg-Val2 Agg-Val3 Agg-Val4 M-S S M D E3 Smith S 18,000 E5 Robinson M 24,000 E9 Graham 18,000 E1 Robson D 32,500 E2 Atkins 24,000 E6 Blakelaw M 54,000 E7 Mortimer D 28,000 E4 Fenwick S 40,000 This example concerns the grouping of rows, not the calculation of aggregate values.

17 Slide 21 Example : Union Operation EmpNo EName M-S Sal EmpNo EName M-S Sal E3 Smith S 18,000 E5 Robinson M 24,000 E9 Graham S E1 Robson D 32,500 E2 Atkins 24,000 E6 Blakelaw M 54,000 E3 Smith S 18,000 E5 Robinson M 24,000 E9 Graham S E1 Robson D 32,500 E2 Atkins 24,000 Union EmpNo EName M-S Sal E1 Robson D 32,500 E2 Atkins 24,000 E6 Blakelaw M 54,000

18 Outer Joins Slide 22 Joins - Inner versus Outer All joins considered so far are Inner Joins. Only a subset of each operand s tuples appear in the result. These are the tuples that match each other in the 2 operands. (Match the comparison (of whatever kind) is true). The tuples don t appear in the result. Sometimes it is useful to have tuples in the result as well. Outer Join Three kinds of Outer Join, to retain in the result all the tuples from : Left operand, Right operand, Left and Right operands. Consider now some illustrations of Inner and Outer Joins. For convenience, a Natural Join is always assumed, but the same principles apply to every type of join.

19 Slide 23 Inner Joins Unmatched tuples are not in the result. (Natural) Slide 24 Outer Join : Left Some tuples are in the result. (Natural)???? padding

20 Slide 25 Outer Join : Right Some tuples are in the result. (Natural) padding???????? Slide 26 Outer Join : Full All tuples are in the result. (Natural)???? padding padding????????

21 Slide 27 Outer Joins in SQL What padding attribute values are used with the columns? SQL uses NULLs. What syntax is used for outer joins? An extension of the FROM phrase inner join syntax. Natural Join, Join Using( ColNames ), Join On( condition ). Each of these can be used for Left, Right and Full outer joins. 9 possibilities. Although 9 possible kinds of outer join may seem a lot to cope with, it is made easier to cope with by remembering that they are the same 3 kinds of joins as for inner joins, and each can be used orthogonally with the 3 kinds of Outer facility; i.e. we decide independently on the kind of join and the kind of Outer facility required, and then just put the two together.

22 Slide 28 SQL2 Outer Natural Joins SELECT * Left Outer optionally inserted FROM R Natural Right Join S ; Full Examples :- SELECT * FROM SUPP Natural Left Outer Join SHIP ; Result retains all the rows of LHS table, i.e. SUPP. SELECT * FROM SUPP Natural Right Join SHIP ; Result retains all the rows of RHS table, i.e. SHIP. Slide 29 The Other Two SQL2 Outer Joins SELECT * Left FROM R Right Join S Using ( attribute(s) ) ; Full Outer SELECT * Left optionally inserted FROM R Right Join S On ( condition ) ; Full Example :- SELECT * FROM SUPP Left Outer Join SHIP Using( S# ) ; Left and right refer to the tables written to left and right of the join operator. Logically only left or right is required, but it is convenient to have both. Useful syntax rules to remember : If outer is optionally used, it always comes after the keyword left/right/full. The keyword(s) left/right/full (outer) always come before the keyword join.

23 Slide 30 Oracle : Outer Joins Original Oracle syntax is completely non-standard. The idea is to add a (+) suffix to the name of the column that is in the table whose columns will receive the NULLs as padding. Regarding left and right, this is the exact opposite of the SQL standard. Example :- SELECT AttributeNames FROM SUPP, SHIP WHERE SUPP.S# = SHIP.S#(+) ; Do NOT use unless desparate! Old fashioned SQL1 join syntax is required. Left & right refer to columns in the WHERE phrase, not tables in the FROM phrase. Full join Union of left and right outer joins. This outer join syntax is peculiar to Oracle and used by no other DBMS. It has several disadvantages, and now that Oracle has provided its DBMSs with an SQL2 standard outer join syntax, you are strongly advised to use that syntax and avoid the old Oracle version. The old version is only mentioned for completeness - only an overview of it is given here - should you find that you need to use an old version of an Oracle DBMS that lacks the modern SQL2 syntax.

Further GroupBy & Extend Operations

Further GroupBy & Extend Operations Slide 1 Further GroupBy & Extend Operations Objectives of the Lecture : To consider whole relation Grouping; To consider the SQL Grouping option Having; To consider the Extend operator & its implementation

More information

Amendments & Transactions

Amendments & Transactions Slide 1 Amendments & Transactions Objectives of the Lecture : To consider amendments to a relation; To consider transactions in SQL. Slide 2 Relational Amendment Strictly speaking, relational amendment

More information

RAQUEL s Relational Operators

RAQUEL s Relational Operators Contents RAQUEL s Relational Operators Introduction 2 General Principles 2 Operator Parameters 3 Ordinary & High-Level Operators 3 Operator Valency 4 Default Tuples 5 The Relational Algebra Operators in

More information

Database Modelling. Lecture 5 Part 1: Updating Database 1/6/2015 1

Database Modelling. Lecture 5 Part 1: Updating Database 1/6/2015 1 Database Modelling Lecture 5 Part 1: Updating Database 1/6/2015 1 Learning Objectives 1. To consider how to do insertions and deletions in SQL 2. To consider amendments (updates) to a relation 3. To consider

More information

Ian Kenny. November 28, 2017

Ian Kenny. November 28, 2017 Ian Kenny November 28, 2017 Introductory Databases Relational Algebra Introduction In this lecture we will cover Relational Algebra. Relational Algebra is the foundation upon which SQL is built and is

More information

CMP-3440 Database Systems

CMP-3440 Database Systems CMP-3440 Database Systems Relational DB Languages Relational Algebra, Calculus, SQL Lecture 05 zain 1 Introduction Relational algebra & relational calculus are formal languages associated with the relational

More information

Information Systems Engineering. SQL Structured Query Language DML Data Manipulation (sub)language

Information Systems Engineering. SQL Structured Query Language DML Data Manipulation (sub)language Information Systems Engineering SQL Structured Query Language DML Data Manipulation (sub)language 1 DML SQL subset for data manipulation (DML) includes four main operations SELECT - used for querying a

More information

Missing Information. We ve assumed every tuple has a value for every attribute. But sometimes information is missing. Two common scenarios:

Missing Information. We ve assumed every tuple has a value for every attribute. But sometimes information is missing. Two common scenarios: NULL values Missing Information We ve assumed every tuple has a value for every attribute. But sometimes information is missing. Two common scenarios: Missing value. E.g., we know a student has some email

More information

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies Databases - 4 Other relational operations and DDL How to write RA expressions for dummies Step 1: Identify the relations required and CP them together Step 2: Add required selections to make the CP Step

More information

Objectives. After completing this lesson, you should be able to do the following:

Objectives. After completing this lesson, you should be able to do the following: Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve Define subqueries List the types of subqueries Write single-row

More information

SQL: Data Manipulation Language. csc343, Introduction to Databases Diane Horton Winter 2017

SQL: Data Manipulation Language. csc343, Introduction to Databases Diane Horton Winter 2017 SQL: Data Manipulation Language csc343, Introduction to Databases Diane Horton Winter 2017 Introduction So far, we have defined database schemas and queries mathematically. SQL is a formal language for

More information

SQL: csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Sina Meraji. Winter 2018

SQL: csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Sina Meraji. Winter 2018 SQL: csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Sina Meraji Winter 2018 Introduction So far, we have defined database schemas and queries mathematically. SQL is a

More information

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

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 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 INSERT INTO DEPT VALUES(4, 'Prog','MO'); The result

More information

Examples of Relational Value Assignments

Examples of Relational Value Assignments Examples of Relational Value Assignments Example Relvars - First Set Let relvar EMP contain sample data of the ID number and name of employees, displayed in tabular format as :- No Name 1 Jack 2 Jill Example

More information

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen LECTURE 10: INTRODUCTION TO SQL FULL RELATIONAL OPERATIONS MODIFICATION LANGUAGE Union, Intersection, Differences (select

More information

RELATIONAL ALGEBRA II. CS121: Relational Databases Fall 2017 Lecture 3

RELATIONAL ALGEBRA II. CS121: Relational Databases Fall 2017 Lecture 3 RELATIONAL ALGEBRA II CS121: Relational Databases Fall 2017 Lecture 3 Last Lecture 2 Query languages provide support for retrieving information from a database Introduced the relational algebra A procedural

More information

CSCB20 Week 4. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

CSCB20 Week 4. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017 CSCB20 Week 4 Introduction to Database and Web Application Programming Anna Bretscher Winter 2017 Last Week Intro to SQL and MySQL Mapping Relational Algebra to SQL queries Focused on queries to start

More information

Databases - 3. Null, Cartesian Product and Join. Null Null is a value that we use when. Something will never have a value

Databases - 3. Null, Cartesian Product and Join. Null Null is a value that we use when. Something will never have a value Databases - 3 Null, Cartesian Product and Join Null Null is a value that we use when Something will never have a value Something will have a value in the future Something had a value but doesn t at the

More information

Subquery: There are basically three types of subqueries are:

Subquery: There are basically three types of subqueries are: Subquery: It is also known as Nested query. Sub queries are queries nested inside other queries, marked off with parentheses, and sometimes referred to as "inner" queries within "outer" queries. Subquery

More information

1. Data Definition Language.

1. Data Definition Language. CSC 468 DBMS Organization Spring 2016 Project, Stage 2, Part 2 FLOPPY SQL This document specifies the version of SQL that FLOPPY must support. We provide the full description of the FLOPPY SQL syntax.

More information

Data Manipulation Language (DML)

Data Manipulation Language (DML) In the name of Allah Islamic University of Gaza Faculty of Engineering Computer Engineering Department ECOM 4113 DataBase Lab Lab # 3 Data Manipulation Language (DML) El-masry 2013 Objective To be familiar

More information

Database Modelling. Lecture 4 (b): Database Integrity, Keys & Constraints. Akhtar Ali 10/14/2014 1

Database Modelling. Lecture 4 (b): Database Integrity, Keys & Constraints. Akhtar Ali 10/14/2014 1 Database Modelling Lecture 4 (b): Database Integrity, Keys & Constraints Akhtar Ali 10/14/2014 1 Learning Objectives 1. To consider Referential Integrity & Foreign Keys 2. To consider Referential Integrity

More information

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL SQL Join Operators Join operation merges rows from two tables and returns the rows with one of the following:

More information

Illustrative Example of Logical Database Creation

Illustrative Example of Logical Database Creation Illustrative Example of Logical Database Creation A small RAQUEL DB is created to illustrate what is involved as regards the logical schemas of a RAQUEL DB. Create a Database or ExampleDB

More information

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL Objectives In this chapter, you will learn: How to use the advanced SQL JOIN operator syntax About the different

More information

Sql Server Syllabus. Overview

Sql Server Syllabus. Overview Sql Server Syllabus Overview This SQL Server training teaches developers all the Transact-SQL skills they need to create database objects like Tables, Views, Stored procedures & Functions and triggers

More information

Databases. Relational Model, Algebra and operations. How do we model and manipulate complex data structures inside a computer system? Until

Databases. Relational Model, Algebra and operations. How do we model and manipulate complex data structures inside a computer system? Until Databases Relational Model, Algebra and operations How do we model and manipulate complex data structures inside a computer system? Until 1970.. Many different views or ways of doing this Could use tree

More information

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data.

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data. DATA 301 Introduction to Data Analytics Relational Databases Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca DATA 301: Data Analytics (2) Why Relational Databases? Relational

More information

SQL: Data Querying. B0B36DBS, BD6B36DBS: Database Systems. h p://www.ksi.m.cuni.cz/~svoboda/courses/172-b0b36dbs/ Lecture 4

SQL: Data Querying. B0B36DBS, BD6B36DBS: Database Systems. h p://www.ksi.m.cuni.cz/~svoboda/courses/172-b0b36dbs/ Lecture 4 B0B36DBS, BD6B36DBS: Database Systems h p://www.ksi.m.cuni.cz/~svoboda/courses/172-b0b36dbs/ Lecture 4 SQL: Data Querying Mar n Svoboda mar n.svoboda@fel.cvut.cz 20. 3. 2018 Czech Technical University

More information

Illustrative Example of Logical Database Creation

Illustrative Example of Logical Database Creation Illustrative Example of Logical Database Creation A small RAQUEL DB is created to illustrate what is involved as regards the logical schemas of a RAQUEL DB. Create a Database or ExampleDB

More information

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL In This Lecture Yet More SQL Database Systems Lecture 9 Natasha Alechina Yet more SQL ORDER BY Aggregate functions and HAVING etc. For more information Connoly and Begg Chapter 5 Ullman and Widom Chapter

More information

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy CompSci 516 Data Intensive Computing Systems Lecture 3 SQL - 2 Instructor: Sudeepa Roy Announcements HW1 reminder: Due on 09/21 (Thurs), 11:55 pm, no late days Project proposal reminder: Due on 09/20 (Wed),

More information

SQL Data Querying and Views

SQL Data Querying and Views Course A7B36DBS: Database Systems Lecture 04: SQL Data Querying and Views Martin Svoboda Faculty of Electrical Engineering, Czech Technical University in Prague Outline SQL Data manipulation SELECT queries

More information

COSC 304 Introduction to Database Systems. Views and Security. Dr. Ramon Lawrence University of British Columbia Okanagan

COSC 304 Introduction to Database Systems. Views and Security. Dr. Ramon Lawrence University of British Columbia Okanagan COSC 304 Introduction to Database Systems Views and Security Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Views A view is a named query that is defined in the database.

More information

Databases - 3. Null, Cartesian Product and Join. Null Null is a value that we use when. Something will never have a value

Databases - 3. Null, Cartesian Product and Join. Null Null is a value that we use when. Something will never have a value Databases - 3, Cartesian Product and Join is a value that we use when Something will never have a value Something will have a value in the future Something had a value but doesn t at the moment is a reserved

More information

Semantic Errors in Database Queries

Semantic Errors in Database Queries Semantic Errors in Database Queries 1 Semantic Errors in Database Queries Stefan Brass TU Clausthal, Germany From April: University of Halle, Germany Semantic Errors in Database Queries 2 Classification

More information

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

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University Lecture 3 SQL Shuigeng Zhou September 23, 2008 School of Computer Science Fudan University Outline Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views

More information

RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague

RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague course: Database Applications (NDBI026) WS2015/16 RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague Triggers Triggers Overview

More information

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

4. SQL - the Relational Database Language Standard 4.3 Data Manipulation Language (DML) Since in the result relation each group is represented by exactly one tuple, in the select clause only aggregate functions can appear, or attributes that are used for grouping, i.e., that are also used

More information

Creating SQL Tables and using Data Types

Creating SQL Tables and using Data Types Creating SQL Tables and using Data Types Aims: To learn how to create tables in Oracle SQL, and how to use Oracle SQL data types in the creation of these tables. Outline of Session: Given a simple database

More information

Announcement. Reading Material. Overview of Query Evaluation. Overview of Query Evaluation. Overview of Query Evaluation 9/26/17

Announcement. Reading Material. Overview of Query Evaluation. Overview of Query Evaluation. Overview of Query Evaluation 9/26/17 Announcement CompSci 516 Database Systems Lecture 10 Query Evaluation and Join Algorithms Project proposal pdf due on sakai by 5 pm, tomorrow, Thursday 09/27 One per group by any member Instructor: Sudeepa

More information

Physical Database Design and Tuning. Chapter 20

Physical Database Design and Tuning. Chapter 20 Physical Database Design and Tuning Chapter 20 Introduction We will be talking at length about database design Conceptual Schema: info to capture, tables, columns, views, etc. Physical Schema: indexes,

More information

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

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) Technology & Information Management Instructor: Michael Kremer, Ph.D. Class 6 Professional Program: Data Administration and Management MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9) AGENDA

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #16 Loops: Matrix Using Nested for Loop In this section, we will use the, for loop to code of the matrix problem.

More information

Enterprise Database Systems

Enterprise Database Systems Enterprise Database Systems Technological Educational Institution of Larissa in collaboration with Staffordshire University Larissa 2006 Dr. Georgia Garani garani@teilar.gr Dr. Theodoros Mitakos teo_ms@yahoo.com

More information

Relational Algebra and SQL

Relational Algebra and SQL Relational Algebra and SQL Computer Science E-66 Harvard University David G. Sullivan, Ph.D. Example Domain: a University We ll use relations from a university database. four relations that store info.

More information

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

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables INDEX Exercise No Title 1 Basic SQL Statements 2 Restricting and Sorting Data 3 Single Row Functions 4 Displaying data from multiple tables 5 Creating and Managing Tables 6 Including Constraints 7 Manipulating

More information

Database Usage (and Construction)

Database Usage (and Construction) Lecture 7 Database Usage (and Construction) More SQL Queries and Relational Algebra Previously Capacity per campus? name capacity campus HB2 186 Johanneberg HC1 105 Johanneberg HC2 115 Johanneberg Jupiter44

More information

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

CMPT 354: Database System I. Lecture 3. SQL Basics CMPT 354: Database System I Lecture 3. SQL Basics 1 Announcements! About Piazza 97 enrolled (as of today) Posts are anonymous to classmates You should have started doing A1 Please come to office hours

More information

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

The SQL data-definition language (DDL) allows defining : Introduction to SQL Introduction to SQL Overview of the SQL Query Language Data Definition Basic Query Structure Additional Basic Operations Set Operations Null Values Aggregate Functions Nested Subqueries

More information

Databases - 5. Problems with the relational model Functions and sub-queries

Databases - 5. Problems with the relational model Functions and sub-queries Databases - 5 Problems with the relational model Functions and sub-queries Problems (1) To store information about real life entities, we often have to cut them up into separate tables Problems (1) To

More information

Key Points. COSC 122 Computer Fluency. Databases. What is a database? Databases in the Real-World DBMS. Database System Approach

Key Points. COSC 122 Computer Fluency. Databases. What is a database? Databases in the Real-World DBMS. Database System Approach COSC 122 Computer Fluency Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Key Points 1) allow for easy storage and retrieval of large amounts of information. 2) Relational

More information

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

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan COSC 304 Introduction to Database Systems SQL Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca SQL Queries Querying with SQL is performed using a SELECT statement. The general

More information

5 Integrity Constraints and Triggers

5 Integrity Constraints and Triggers 5 Integrity Constraints and Triggers 5.1 Integrity Constraints In Section 1 we have discussed three types of integrity constraints: not null constraints, primary keys, and unique constraints. In this section

More information

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances COSC 304 Introduction to Database Systems SQL Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca SQL Queries Querying with SQL is performed using a SELECT statement. The general

More information

SQL STRUCTURED QUERY LANGUAGE

SQL STRUCTURED QUERY LANGUAGE STRUCTURED QUERY LANGUAGE SQL Structured Query Language 4.1 Introduction Originally, SQL was called SEQUEL (for Structured English QUery Language) and implemented at IBM Research as the interface for an

More information

Generalising Relational Algebra Set Operators

Generalising Relational Algebra Set Operators Generalising Relational lgebra Set Operators Introduction The relational algebra join operators, Natural Join and Generalised (or Theta) Join, can both be generalised to so that they incorporate semi joins

More information

Physical Database Design and Tuning. Review - Normal Forms. Review: Normal Forms. Introduction. Understanding the Workload. Creating an ISUD Chart

Physical Database Design and Tuning. Review - Normal Forms. Review: Normal Forms. Introduction. Understanding the Workload. Creating an ISUD Chart Physical Database Design and Tuning R&G - Chapter 20 Although the whole of this life were said to be nothing but a dream and the physical world nothing but a phantasm, I should call this dream or phantasm

More information

SQL Data Query Language

SQL Data Query Language SQL Data Query Language André Restivo 1 / 68 Index Introduction Selecting Data Choosing Columns Filtering Rows Set Operators Joining Tables Aggregating Data Sorting Rows Limiting Data Text Operators Nested

More information

Visit for more.

Visit  for more. Chapter 9: More On Database & SQL Advanced Concepts Informatics Practices Class XII (CBSE Board) Revised as per CBSE Curriculum 2015 Visit www.ip4you.blogspot.com for more. Authored By:- Rajesh Kumar Mishra,

More information

Introduction to database design

Introduction to database design Introduction to database design First lecture: RG 3.6, 3.7, [4], most of 5 Second lecture: Rest of RG 5 Rasmus Pagh Some figures are taken from the ppt slides from the book Database systems by Kiefer,

More information

CHAPTER 4 MISSING INFORMATION ADVANCED DATABASE SYSTEMS. Assist. Prof. Dr. Volkan TUNALI

CHAPTER 4 MISSING INFORMATION ADVANCED DATABASE SYSTEMS. Assist. Prof. Dr. Volkan TUNALI CHAPTER 4 MISSING INFORMATION ADVANCED DATABASE SYSTEMS Assist. Prof. Dr. Volkan TUNALI Topics 2 Introduction The 3VL Approach NULLs and Keys Outer Join SQL Facilities Introduction 3 Information is often

More information

SQL Part 3: Where Subqueries and other Syntactic Sugar Part 4: Unknown Values and NULLs

SQL Part 3: Where Subqueries and other Syntactic Sugar Part 4: Unknown Values and NULLs SQL Part 3: Where Subqueries and other Syntactic Sugar Part 4: Unknown Values and NULLs 1-1 List of Slides 1 2 More on "where" conditions 3 Esoteric Predicates: Example 4 WHERE Subqueries 5 Overview of

More information

Database Management Systems,

Database Management Systems, Database Management Systems SQL Query Language (3) 1 Topics Aggregate Functions in Queries count sum max min avg Group by queries Set Operations in SQL Queries Views 2 Aggregate Functions Tables are collections

More information

SQL - Lecture 3 (Aggregation, etc.)

SQL - Lecture 3 (Aggregation, etc.) SQL - Lecture 3 (Aggregation, etc.) INFS 614 INFS614 1 Example Instances S1 S2 R1 sid bid day 22 101 10/10/96 58 103 11/12/96 sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 sid

More information

Chapter 3: Introduction to SQL

Chapter 3: Introduction to SQL Chapter 3: Introduction to SQL Database System Concepts, 6 th Ed. See www.db-book.com for conditions on re-use Chapter 3: Introduction to SQL Overview of the SQL Query Language Data Definition Basic Query

More information

SQL Structured Query Language Introduction

SQL Structured Query Language Introduction SQL Structured Query Language Introduction Rifat Shahriyar Dept of CSE, BUET Tables In relational database systems data are represented using tables (relations). A query issued against the database also

More information

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

Simple queries Set operations Aggregate operators Null values Joins Query Optimization. John Edgar 2 CMPT 354 Simple queries Set operations Aggregate operators Null values Joins Query Optimization John Edgar 2 Data Manipulation Language (DML) to Write queries Insert, delete and modify records Data Definition

More information

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept] Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept] 1. What is DBMS? A Database Management System (DBMS) is a program that controls creation, maintenance and use

More information

Relational Model, Relational Algebra, and SQL

Relational Model, Relational Algebra, and SQL Relational Model, Relational Algebra, and SQL August 29, 2007 1 Relational Model Data model. constraints. Set of conceptual tools for describing of data, data semantics, data relationships, and data integrity

More information

Chapter 16: Advanced MySQL- Grouping Records and Joining Tables. Informatics Practices Class XII. By- Rajesh Kumar Mishra

Chapter 16: Advanced MySQL- Grouping Records and Joining Tables. Informatics Practices Class XII. By- Rajesh Kumar Mishra Chapter 16: Advanced MySQL- Grouping Records and Joining Tables Informatics Practices Class XII By- Rajesh Kumar Mishra PGT (Comp.Sc.) KV No.1, AFS, Suratgarh (Raj.) e-mail : rkmalld@gmail.com Grouping

More information

Lecture 3 More SQL. Instructor: Sudeepa Roy. CompSci 516: Database Systems

Lecture 3 More SQL. Instructor: Sudeepa Roy. CompSci 516: Database Systems CompSci 516 Database Systems Lecture 3 More SQL Instructor: Sudeepa Roy Duke CS, Fall 2018 CompSci 516: Database Systems 1 Announcements HW1 is published on Sakai: Resources -> HW -> HW1 folder Due on

More information

Relational Database Management Systems for Epidemiologists: SQL Part I

Relational Database Management Systems for Epidemiologists: SQL Part I Relational Database Management Systems for Epidemiologists: SQL Part I Outline SQL Basics Retrieving Data from a Table Operators and Functions What is SQL? SQL is the standard programming language to create,

More information

Relational Database Management Systems for Epidemiologists: SQL Part II

Relational Database Management Systems for Epidemiologists: SQL Part II Relational Database Management Systems for Epidemiologists: SQL Part II Outline Summarizing and Grouping Data Retrieving Data from Multiple Tables using JOINS Summary of Aggregate Functions Function MIN

More information

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

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement GIFT Department of Computing Science [Spring 2013] CS-217: Database Systems Lab-2 Manual Data Selection and Filtering using the SELECT Statement V1.0 4/12/2016 Introduction to Lab-2 This lab reinforces

More information

Querying Data with Transact SQL

Querying Data with Transact SQL Course 20761A: Querying Data with Transact SQL Course details Course Outline Module 1: Introduction to Microsoft SQL Server 2016 This module introduces SQL Server, the versions of SQL Server, including

More information

RELATIONAL DATA MODEL: Relational Algebra

RELATIONAL DATA MODEL: Relational Algebra RELATIONAL DATA MODEL: Relational Algebra Outline 1. Relational Algebra 2. Relational Algebra Example Queries 1. Relational Algebra A basic set of relational model operations constitute the relational

More information

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

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql ASSIGNMENT NO. 3 Title: Design at least 10 SQL queries for suitable database application using SQL DML statements: Insert, Select, Update, Delete with operators, functions, and set operator. Requirements:

More information

CS 252: Fundamentals of Relational Databases: SQL5

CS 252: Fundamentals of Relational Databases: SQL5 CS 252: Fundamentals of Relational Databases: SQL5 Dr. Alexandra I. Cristea http://www.dcs.warwick.ac.uk/~acristea/ Careful study of these notes is best left until most of the lectures on CS252 have been

More information

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

T-SQL Training: T-SQL for SQL Server for Developers Duration: 3 days T-SQL Training Overview T-SQL for SQL Server for Developers training teaches developers all the Transact-SQL skills they need to develop queries and views, and manipulate data in a SQL

More information

Slicing and Dicing Data in CF and SQL: Part 1

Slicing and Dicing Data in CF and SQL: Part 1 Slicing and Dicing Data in CF and SQL: Part 1 Charlie Arehart Founder/CTO Systemanage carehart@systemanage.com SysteManage: Agenda Slicing and Dicing Data in Many Ways Handling Distinct Column Values Manipulating

More information

Administrivia. Physical Database Design. Review: Optimization Strategies. Review: Query Optimization. Review: Database Design

Administrivia. Physical Database Design. Review: Optimization Strategies. Review: Query Optimization. Review: Database Design Administrivia Physical Database Design R&G Chapter 16 Lecture 26 Homework 5 available Due Monday, December 8 Assignment has more details since first release Large data files now available No class Thursday,

More information

School of Computing, Engineering and Information Sciences University of Northumbria. Set Operations

School of Computing, Engineering and Information Sciences University of Northumbria. Set Operations Set Operations Aim: To understand how to do the equivalent of the UNION, DIFFERENCE and INTERSECT set operations in SQL. Outline of Session: Do some example SQL queries to learn to differentiate between

More information

CSC Web Programming. Introduction to SQL

CSC Web Programming. Introduction to SQL CSC 242 - Web Programming Introduction to SQL SQL Statements Data Definition Language CREATE ALTER DROP Data Manipulation Language INSERT UPDATE DELETE Data Query Language SELECT SQL statements end with

More information

CS2 Current Technologies Note 1 CS2Bh

CS2 Current Technologies Note 1 CS2Bh CS2 Current Technologies Note 1 Relational Database Systems Introduction When we wish to extract information from a database, we communicate with the Database Management System (DBMS) using a query language

More information

SQL. SQL DDL Statements

SQL. SQL DDL Statements SQL Structured Query Language Declarative Specify the properties that should hold in the result, not how to obtain the result Complex queries have procedural elements International Standard SQL1 (1986)

More information

The Extended Algebra. Duplicate Elimination. Sorting. Example: Duplicate Elimination

The Extended Algebra. Duplicate Elimination. Sorting. Example: Duplicate Elimination The Extended Algebra Duplicate Elimination 2 δ = eliminate duplicates from bags. τ = sort tuples. γ = grouping and aggregation. Outerjoin : avoids dangling tuples = tuples that do not join with anything.

More information

Test Bank for Database Processing Fundamentals Design and Implementation 13th Edition by Kroenke

Test Bank for Database Processing Fundamentals Design and Implementation 13th Edition by Kroenke Test Bank for Database Processing Fundamentals Design and Implementation 13th Edition by Kroenke Link full download: https://testbankservice.com/download/test-bank-fordatabase-processing-fundamentals-design-and-implementation-13th-edition-bykroenke

More information

1) Introduction to SQL

1) Introduction to SQL 1) Introduction to SQL a) Database language enables users to: i) Create the database and relation structure; ii) Perform insertion, modification and deletion of data from the relationship; and iii) Perform

More information

QQ Group

QQ Group QQ Group: 617230453 1 Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join 2 Generalized Projection Extends the projection operation by allowing arithmetic functions

More information

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects.

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects. Set Theory Set theory is a branch of mathematics that studies sets. Sets are a collection of objects. Often, all members of a set have similar properties, such as odd numbers less than 10 or students in

More information

Full file at

Full file at David Kroenke's Database Processing: Fundamentals, Design and Implementation (10 th Edition) CHAPTER TWO INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) True-False Questions 1. SQL stands for Standard

More information

Relational Model History. COSC 304 Introduction to Database Systems. Relational Model and Algebra. Relational Model Definitions.

Relational Model History. COSC 304 Introduction to Database Systems. Relational Model and Algebra. Relational Model Definitions. COSC 304 Introduction to Database Systems Relational Model and Algebra Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Relational Model History The relational model was

More information

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

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL Chapter 3: Introduction to SQL Database System Concepts, 6 th Ed. See www.db-book.com for conditions on re-use Chapter 3: Introduction to SQL Overview of The SQL Query Language Data Definition Basic Query

More information

Programming the Database

Programming the Database Programming the Database Today s Lecture 1. Stored Procedures 2. Functions BBM471 Database Management Systems Dr. Fuat Akal akal@hacettepe.edu.tr 3. Cursors 4. Triggers 5. Dynamic SQL 2 Stored Procedures

More information

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13 CS121 MIDTERM REVIEW CS121: Relational Databases Fall 2017 Lecture 13 2 Before We Start Midterm Overview 3 6 hours, multiple sittings Open book, open notes, open lecture slides No collaboration Possible

More information

SQL Interview Questions

SQL Interview Questions SQL Interview Questions SQL stands for Structured Query Language. It is used as a programming language for querying Relational Database Management Systems. In this tutorial, we shall go through the basic

More information

Chapter 3: Introduction to SQL

Chapter 3: Introduction to SQL Chapter 3: Introduction to SQL Database System Concepts, 6 th Ed. See www.db-book.com for conditions on re-use Chapter 3: Introduction to SQL Overview of the SQL Query Language Data Definition Basic Query

More information

Relational Algebra and SQL

Relational Algebra and SQL Relational Algebra and SQL Relational Algebra. This algebra is an important form of query language for the relational model. The operators of the relational algebra: divided into the following classes:

More information

Views. COSC 304 Introduction to Database Systems. Views and Security. Creating Views. Views Example. Removing Views.

Views. COSC 304 Introduction to Database Systems. Views and Security. Creating Views. Views Example. Removing Views. COSC 304 Introduction to Database Systems Views and Security Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Views A view is a named query that is defined in the database.

More information