SQL Querying: Understanding SELECT Statements and Related Concepts, Slides of Database Management Systems (DBMS)

An in-depth exploration of sql select statements, including examples, operational semantics, renaming attributes, expressions in select clauses, complex conditions in where clauses, patterns, null values, three-valued logic, subqueries, the in and exists operators, the any and all operators, and aggregations. It covers various sql concepts and queries to help readers understand how to effectively use select statements.

Typology: Slides

2012/2013

Uploaded on 04/26/2013

duurga
duurga 🇮🇳

4.6

(25)

121 documents

1 / 69

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to SQL
Select-From-Where Statements
Subqueries
Grouping and Aggregation
1
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
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45

Partial preview of the text

Download SQL Querying: Understanding SELECT Statements and Related Concepts and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Introduction to SQL

Select-From-Where Statements

Subqueries Grouping and Aggregation

Why SQL?

  • SQL is a very-high-level language.
    • Say “what to do” rather than “how to do it.”
    • Avoid a lot of data-manipulation details needed in procedural languages like C++ or Java.
  • Database management system figures out “best” way to execute query. - Called “query optimization.”

Our Running Example

  • All our SQL queries will be based on the following database schema. - Underline indicates key attributes. Candies(name, manf) Stores(name, addr, license) Consumers(name, addr, phone) Likes(consumer, candy) Sells(store, candy, price) Frequents(consumer, store)

Example

  • Using Candies(name, manf), what candies are made by Hershey? SELECT name FROM Candies WHERE manf = ’Hershey’;

5

Notice SQL uses single-quotes for strings. SQL is case-insensitive, except inside strings.

Meaning of Single-Relation Query

  • Begin with the relation in the FROM clause.
  • Apply the selection indicated by the WHERE clause.
  • Apply the extended projection indicated by the SELECT clause.

Operational Semantics

  • To implement this algorithm think of a tuple variable (tv) ranging over each tuple of the relation mentioned in FROM.
  • Check if the “current” tuple satisfies the WHERE clause.
  • If so, compute the attributes or expressions of the SELECT clause using the components of this tuple.

* In SELECT clauses

  • When there is one relation in the FROM clause,
    • in the SELECT clause stands for “all attributes of this relation.”
  • Example using Candies(name, manf):

SELECT * FROM Candies WHERE manf = ’Hershey’;

Result of Query:

name manf Twizzler Hershey Kitkat Hershey AlmondJoyHershey

......

11

Now, the result has each of the attributes of Candies.

Result of Query:

candy manf

Twizzler Hershey

Kitkat Hershey

AlmondJoyHershey

......

Expressions in SELECT Clauses

  • Any expression that makes sense can appear as an element of a SELECT clause.
  • Example: from Sells(store, candy, price):

SELECT store, candy, price * 114 AS priceInYen FROM Sells;

Another Example: Constant

Expressions

  • From Likes(consumer, candy) :

SELECT consumer,’likes Kitkats’ AS whoLikesKitkats FROM Likes WHERE candy = ’Kitkat’;

Result of Query

consumer whoLikesKitkats

Sally likes Kitkats

Fred likes Kitkats

… …

Patterns

  • WHERE clauses can have conditions in which a string is compared with a pattern, to see if it matches.
  • General form: LIKE or NOT LIKE
  • Pattern is a quoted string with % = “any string”; _ = “any character.”

Example

  • From Consumers(name, addr, phone) find the consumers with exchange 555:

SELECT name

FROM Consumers

WHERE phone LIKE ’%555-_ _ _ _’;