Relational Algebra & SQL: Unary/Binary Operators, Single Table Queries & Subqueries, Slides of Database Management Systems (DBMS)

An overview of relational algebra and structured query language (sql). It covers unary and binary operators in relational algebra, sql queries on a single table, sql queries on binary operators, and subqueries. The use of select and project operators in relational algebra, cartesian product, union, intersection, difference, natural join, equijoin, and theta join operators. In sql, the document discusses the select statement, its clauses, and various sql queries based on a single table, such as handling null values, using logical operators, and aggregate functions.

Typology: Slides

2012/2013

Uploaded on 04/27/2013

arunima
arunima 🇮🇳

3

(2)

99 documents

1 / 31

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 11.1 and 11.2
Data Manipulation: Relational Algebra and SQL
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download Relational Algebra & SQL: Unary/Binary Operators, Single Table Queries & Subqueries and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Chapter 11.1 and 11.

Data Manipulation: Relational Algebra and SQL

Agenda

  • 11.1 – Relational Algebra
    • Unary Operators
    • Binary Operators
  • 11.2 – Structured Query Language (SQL)
    • SQL Queries on a Single Table
    • SQL Queries on Binary Operators
    • Subqueries

Relational Algebra

• Binary Operators

  • Cartesian Product Operator
  • Set Theoretic Operators
    • Union
    • Intersection
    • Difference
  • Join Operators
    • Natural Join Operator
    • Equijoin Operator
    • Theta Join Operator
    • Outer Join Operator
  • The Divide Operator

Relational Algebra

• Binary Operators

  • Cartesian Product Operator is used to combine tuples from any two relations in a combinatorial fashion
  • Relational Algebra Syntax: RELATION_R X RELATION_S

Relational Algebra

• Join Operators

  • Join Operators specify how to relate tables in the query, it is used to combine related tuples from two relations into single tuples
  • In order to join the two relations R and S, they must be join compatible, which means that the join condition must involve attributes from R and S which share the same domain

Relational Algebra

• Join Operators

  • Natural Join Operator produces all the different combinations of the tuples from the two relations R and S that satisfy a join condition R * (^) S

Relational Algebra

• Join Operators

  • Theta Join Operator produces the combinations of tuples from R and S that satisfy a join condition which does not have to involve equality comparisons (if it did then they are called an Equijoin) R [X] S
  • Let R be the CAR relation and S be the BOAT relation if a customer wants to buy both a car and boat but doesn’t want to spend more money on a boat then a car we would use a theta join operator to give a new relation with all the possible options CarPrice ≥ BoatPrice

Relational Algebra

• Join Operators

  • Outer Join Operators are Inner Join operations that only look at matching tuples from one relation to the other, attributes of tuples with no matching tuples are set to NULL
  • Left Outer Join is denoted by ]X| , and keeps every tuple in the left or first relation (R)
  • Right Outer Join is denoted by |X[ , and keeps every tuple in the right or second relation (S)
  • Full Outer Join is denoted by ]X[ , and keeps all the tuples in both the left and right relations when no matching tuples are found

Relational Algebra

• Divide Operator

  • Divide Operator returns every tuple from R that match all tuples in S; R and S must be division compatible R ÷ S
  • Let R be COMPLETED and S be DBPROJECT we can get the result as

Structured Query Language (SQL)

• Overview

• SQL Queries based on a Single Table

  • Sample SQL Queries
  • Handling null values

• SQL Queries based on Binary Operators

• Subqueries

Structured Query Language (SQL)

• Overview

  • Other useful clauses for a SELECT statement include: GROUP BY group_by_expression - Forms groups of rows with the same value HAVING group_condition - Filters the groups subject to some condition ORDER BY column name(s) - Specifies the order of the output
  • After your SELECT statement has been written a semi-colon ‘;’ is needed at the end to close up the statement

Structured Query Language (SQL)

• SQL Queries Based on a Single Table

  • We can use an asterisk (*) to denote that all columns from a table is to be selected
  • We can also use the logical operators AND OR and NOT with the WHERE clause to form more precise clauses
  • When writing a query the hierarchy order in any expression follows
  1. The comparison operators (> ≥ = ≠ ≤ <) = is equals <> is not equals

    = is greater than or equal to <= is less than or equal to Is greater than < is less than

  2. NOT operator
  3. AND operator
  4. OR operator

Structured Query Language (SQL)

• SQL Queries Based on a Single Table

Example 2 Suppose we want to know all professors who make more than $6000 a month and the professors who work in a certain department (known as code 3) SELECT * FROM PROFESSOR WHERE PROFESSOR.SALARY/12 > 6000 AND PROFESSOR.PR_DPT_DCODE = 3;

Structured Query Language (SQL)

• SQL Queries Based on a Single Table

  • We can also simulate a projection operation by using SELECT
  • We can get all the colleges of a university from the co_college column with this command SELECT COURSE.CO_COLLEGE FROM COURSE;