Docsity

SQL Queries in Enterprise Computing - Prof. Mark Llewellyn, Study notes of Computer Science

Partial preview of the text

Download SQL Queries in Enterprise Computing - Prof. Mark Llewellyn and more Study notes Computer Science in PDF only on Docsity!

CNT 4714: Enterprise Computing

Fall 2009

SQL Queries

School of Electrical Engineering and Computer Science University of Central Florida

Instructor : Dr. Mark Llewellyn [email protected] HEC 236, 407-823- http://www.cs.ucf.edu/courses/cnt4714/fall

The SQL Environment

  • Catalog
    • A set of schemas that constitute the description of a database.
  • Schema
    • The structure that contains descriptions of objects created by a

user (base tables, views, constraints).

  • Data Definition Language (DDL)
    • Commands that define a database, including creating, altering,

and dropping tables and establishing constraints.

  • Data Manipulation Language (DML)
    • Commands that maintain and query a database.
  • Data Control Language (DCL)
    • Commands that control a database, including administering

privileges and committing data.

SELECT Statement

  • Used for queries on single or multiple tables.
  • Clauses of the SELECT statement:
    • SELECT
      • List the columns (and expressions) that should be returned from the query
    • FROM
      • Indicate the table(s) or view(s) from which data will be obtained
    • WHERE
      • Indicate the conditions under which a row will be included in the result
    • GROUP BY
      • Indicate categorization of results
    • HAVING
      • Indicate the conditions under which a category (group) will be included
    • ORDER BY
      • Sorts the result according to specified criteria

SQL statement

processing order

SELECT Example

  • Find products with standard price less than $

SELECT PRODUCT_NAME, STANDARD_PRICE FROM PRODUCT_V WHERE STANDARD_PRICE < 275;

SELECT Example using Alias

  • Alias is an alternative column or table name.

SELECT CUST.CUSTOMER AS NAME,

CUST.CUSTOMER_ADDRESS

FROM CUSTOMER_V CUST

WHERE NAME = „Home Furnishings‟;

SELECT Example – Boolean Operators

  • AND, OR, and NOT Operators for customizing

conditions in WHERE clause

SELECT PRODUCT_DESCRIPTION, PRODUCT_FINISH, STANDARD_PRICE FROM PRODUCT_V WHERE (PRODUCT_DESCRIPTION LIKE „%Desk‟ OR PRODUCT_DESCRIPTION LIKE „%Table‟) AND UNIT_PRICE > 300;

Note: the LIKE operator allows you to compare strings using wildcards. For example, the % wildcard in „%Desk‟ indicates that all strings that have any number of characters preceding the word “Desk” will be allowed

SELECT Example –

Sorting Results with the ORDER BY Clause

  • Sort the results first by STATE, and within a state

by CUSTOMER_NAME

SELECT CUSTOMER_NAME, CITY, STATE

FROM CUSTOMER_V

WHERE STATE IN („FL‟, „TX‟, „CA‟, „HI‟)

ORDER BY STATE, CUSTOMER_NAME;

Note: the IN operator in this example allows you to include rows whose STATE value is either FL, TX, CA, or HI. It is more efficient than separate OR conditions

SELECT Example –

Qualifying Results by Category Using the HAVING Clause

• For use with GROUP BY

SELECT STATE, COUNT(STATE)

FROM CUSTOMER_V

GROUP BY STATE

HAVING COUNT(STATE) > 1;

Like a WHERE clause, but it operates on groups (categories), not on individual rows. Here, only those groups with total numbers greater than 1 will be included in final result

Processing Multiple Tables – Joins

  • Join – a relational operation that causes two or more tables with a

common domain to be combined into a single table or view

  • Equi-join – a join in which the joining condition is based on equality between values in the common columns; common columns appear redundantly in the result table
  • Natural join – an equi-join in which one of the duplicate columns is eliminated in the result table
  • Outer join – a join in which rows that do not have matching values in common columns are nonetheless included in the result table (as opposed to inner join, in which rows must have matching values in order to appear in the result table)
  • Union join – includes all columns from each table in the join, and an instance for each row of each table

The common columns in joined tables are usually the primary key of the dominant table and the foreign key of the dependent table in 1:M relationships

  • List the customer name, ID number, and order number

for all customers. Include customer information even

for customers that do have an order

SELECT CUSTOMER_T.CUSTOMER_ID, CUSTOMER_NAME,
ORDER_ID
FROM CUSTOMER_T, LEFT OUTER JOIN ORDER_T
ON CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID;

Outer Join Example

LEFT OUTER JOIN syntax with ON keyword instead of WHERE  causes customer data to appear even if there is no corresponding order data

  • Assemble all information necessary to create an invoice

for order number 1006

SELECT CUSTOMER_T.CUSTOMER_ID, CUSTOMER_NAME,
CUSTOMER_ADDRESS, CITY, SATE, POSTAL_CODE,
ORDER_T.ORDER_ID, ORDER_DATE, QUANTITY,
PRODUCT_NAME, UNIT_PRICE, (QUANTITY * UNIT_PRICE)
FROM CUSTOMER_T, ORDER_T, ORDER_LINE_T, PRODUCT_T
WHERE CUSTOMER_T.CUSTOMER_ID =
ORDER_LINE.CUSTOMER_ID AND ORDER_T.ORDER_ID =
ORDER_LINE_T.ORDER_ID
AND ORDER_LINE_T.PRODUCT_ID =
PRODUCT_PRODUCT_ID
AND ORDER_T.ORDER_ID = 1006;

Four tables involved in this join

Multiple Table Join Example

Each pair of tables requires an equality-check condition in the WHERE clause, matching primary keys against foreign keys

  • Show all customers who have placed an order.
SELECT CUSTOMER_NAME FROM CUSTOMER_T
WHERE CUSTOMER_ID IN
(SELECT DISTINCT CUSTOMER_ID FROM ORDER_T);

Subquery Example

Subquery is embedded in parentheses. In this case it returns a list that will be used in the WHERE clause of the outer query

The IN operator will test to see if the CUSTOMER_ID value of a row is included in the list returned from the subquery

Correlated vs. Noncorrelated Subqueries

  • Noncorrelated subqueries:
    • Do not depend on data from the outer query.
    • Execute once for the entire outer query.
  • Correlated subqueries:
    • Make use of data from the outer query.
    • Execute once for each row of the outer query.
    • Can use the EXISTS operator.