0% found this document useful (0 votes)
44 views36 pages

10 - Chapter 6 - Simple Queries in SQL

This document provides an overview of SQL (Structured Query Language) and how to write simple queries. It discusses SQL projections to select specific columns, SQL selections to filter rows using a WHERE clause, comparing and pattern matching on strings, handling date/time data types, and working with NULL values. The document also provides examples of basic SQL queries using projections, selections, string comparisons, and pattern matching.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
44 views36 pages

10 - Chapter 6 - Simple Queries in SQL

This document provides an overview of SQL (Structured Query Language) and how to write simple queries. It discusses SQL projections to select specific columns, SQL selections to filter rows using a WHERE clause, comparing and pattern matching on strings, handling date/time data types, and working with NULL values. The document also provides examples of basic SQL queries using projections, selections, string comparisons, and pattern matching.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 36

Database Systems

Session 11
Chapter 6 – The Database Language SQL
Simple Queries in SQL
Objectives

1 Understand how to write simple queries

2 Understand about NULL values

3 Understand about the string data type, date-time data type

4 Know how to order the output


Contents

1 SQL Overview

2 Projection in SQL

3 Selection in SQL

4 Comparison of Strings and Pattern matching in SQL

5 Date time data type

6 NULL values

7 Ordering the output


SQL Overview

 The most commonly used relational DBMS’s query and


modify the database through a language called SQL.

 SQL (stands for Structured Query Language) is the set of


statements with which all programs and users access data
in an database.

 The language, Structured English Query Language


("SEQUEL") was developed by IBM Corporation. SEQUEL
later became SQL (still pronounced "sequel").

 Today, SQL is accepted as the standard RDBMS


language.
SQL Revisions

Year Name Alias Comments


1986 SQL-86 SQL-87 First published by ANSI. Ratified by ISO in 1987.

1989 SQL-89 FIPS 127-1 Minor revision, adopted as FIPS 127-1.

1992 SQL-92 SQL2, FIPS 127-2 Major revision (ISO 9075), Entry Level SQL-92 adopted as FIPS 127-2.

1999 SQL:1999 SQL3 Added regular expression matching, recursive queries, triggers, support for procedural
and control-of-flow statements, non-scalar types, and some object-oriented features.

2003 SQL:2003   Introduced XML-related features, window functions, standardized sequences, and
columns with auto-generated values (including identity-columns).

2006 SQL:2006   ISO/IEC 9075-14:2006 defines ways in which SQL can be used in conjunction with
XML. It defines ways of importing and storing XML data in an SQL database,
manipulating it within the database and publishing both XML and conventional SQL-
data in XML form. In addition, it provides facilities that permit applications to
integrate into their SQL code the use of XQuery, the XML Query Language published
by the World Wide Web Consortium (W3C), to concurrently access ordinary SQL-data
and XML documents.

2008 SQL:2008   Defines more flexible windowing functions, clarifies SQL 2003 items that were still
unclear [1]
Transact SQL (T-SQL)

Transact-SQL (T-SQL) is Microsoft's


and Sybase's proprietary extension to
SQL
T-SQL

SQL
Sub-languages of T-SQL

T - SQL

DDL
(Data Definition Language)

DML
(Data Manipulation Language)

DCL
(Data Control Language)
Sub-languages of T-SQL
SELECT commands

A SELECT statement retrieves information from the database. Using a


SELECT statement, you can do the following:
 Projection: You can use the projection capability in SQL to choose the
columns in a table that you want returned by your query.
 Selection: You can use the selection capability in SQL to choose the rows
in a table that you want returned by a query (with WHERE clause)
 Joining: You can use the join capability in SQL to bring together data that is
stored in different tables by creating a link between them.
Notes

 A keyword refers to an individual SQL element.


For example, SELECT and FROM are keywords.
 A clause is a part of a SQL statement.
For example, SELECT employee_id, last_name, ...
is a clause.
 A statement is a combination of two or more
clauses.
For example, SELECT * FROM employees is a
SQL statement.
Basic Syntax for a simple SELECT queries

SELECT [ ALL | DISTINCT ]


    [ TOP n [ PERCENT ] ]
* | {column_name | expression [alias],…}
FROM tableName
[WHERE conditional expressions]

 SELECT identifies which attributes are produced as part of the answer


 ALL: Specifies that duplicate rows can appear in the result set. ALL is the default
 DISTINCT: Specifies that only unique rows can appear in the result set. Null values are
considered equal for the purposes of the DISTINCT keyword
 TOP n [ PERCENT ]:Specifies that only the first n rows are to be output from the query
result set. n is an integer between 0 and 4294967295. If PERCENT is also specified, only
the first n percent of the rows are output from the result set. When specified with PERCENT,

n must be an integer between 0 and 100


 FROM identifies which table
 WHERE identifies the conditions for filtering the results, like selection-condition in
relational algebra
A trick for reading & writing queries

 It’s generally easiest to examine a SELECT-


FROM-WHERE query by:
 First looking at the FROM clause to learn which
relations are involved in the query
 Then, move to the WHERE clause to learn what it is
about tuples that is important to the query
 Finally, look at the SELECT clause to see what the
output format is
 The same order: FROM, then WHERE, then
SELECT is often useful when writing queries of
your own as well
Example: SELECT all columns
Projection in SQL

 We can, if we wish, eliminate some of the


components of the chosen tuples; that is, we
can project the relation produced by a SQL
query onto some of its attributes
 In place of the * of the SELECT clause, we may
list some of the attributes of the relation
mentioned in the FROM clause. The result will
be projected onto the attributes listed

SELECT ColumnName1, ColumnName2, …


FROM tableName
Example: Projection in SQL
Projection in SQL: Renaming Column headers

We could rename a column header in the


result by using column alias
Follow the name of attribute by the
keyword as and an alias.

SELECT ColumnName1 as Alias1, ColumnName2 as Alias 2, …


FROM tableName
Example: Renaming column headers

The example displays the “Name” as “Product’s Name”


Because “Product’s Name” contain a space, it has been
enclosed in double quotation marks.
Projection in SQL: extended projection

 In the SELECT cause, we could create new


attribute by using an expression
 We should give an alias for the column
corresponding to the expression

SELECT ColumnName1, …, expression1 as alias1,…


FROM tableName
Example: Extended projection

 Note that the resultant calculated column “Price In VND” is not a new
column in the “Products” table; it is for display only.
Duplication Eliminating with
SELECT distinct
Selection in SQL (Restricting data)

While retrieving data from the database,


you may need to restrict the rows of data
that are displayed
In that case, the solution is to use the
WHERE clause
The WHERE clause is equal to the
selection operator of relational algebra
The expression that may follow WHERE
include conditional expressions like those
found in C or Java
Selection in SQL (or Restricting data)

SELECT [ ALL | DISTINCT ]


    [ TOP n [ PERCENT ] ]
* | {column_name | expression [alias],…}
[FROM tableName]
[WHERE conditional expressions]

 WHERE: restricts the query to rows that meet


conditions expressed by conditional expressions
 Conditional expression: is composed of column
names, expressions, constants, and a comparison
operator
Example: Restricting data
Example: Restricting data
Comparison of Strings

 Two strings are equal if they are the same


sequence of characters.
 When comparing strings with different
declarations, only the actual strings are
compared (SQL ignores any “pad” characters
that must be presenet in the database in order to
give a string its required length)
 We can use “<“, “>”, “=“, “<=”, “>=” and “<>”
operators to compare two strings
Pattern matching in SQL

An alternative form of comparison


expression is:
s LIKE p
where:
 S: is a string
 P: is a pattern (with the optional use of some
special characters: “%”, “_” ..)
• % corresponds to a sequence of characters
• _ corresponds to any character
Similarly, “s NOT LIKE p” is true if and
only if string s does not match pattern p
Example: Pattern matching in SQL

List title of the movies having title being


with ‘Star’ and a blank.

List title of the movies having title with 5


characters long

Sequence of five ‘_’ characters


Example: Pattern matching in SQL

List title of the movies begin with a S or a


G.
Dates and Times

 SQL generally support dates and times as


special data types. These values are often
representable in a variety of formats such as:
 Date: ‘05/14/1948’, ’14 May 1948’, ‘1948-05-14’
 Time: ’15:00:02’, ’16:10:25.5’
 DateTime: ‘1948-05-14 12:00:02’
 We can compare dates or times using the same
comparison operators we use for numbers or
strings (<, >, <=, >=, =, <>)
NULL values

 Null is a special marker used in Structured Query Language


(SQL) to indicate that a data value does not exist in the
database.
 Since Null is not a member of any data domain, it is not
considered a "value", but rather a marker (or placeholder)
indicating the absence of value.
 When we operate on a NULL and any value, using an arithmetic
operator like *, +, -, …, the result is NULL.

 When we compare a NULL value and any value, using a comparison


operator like = or >, the result is UNKNOWN

 We could use expression “x is NULL” to ask if x has the value


NULL
The Truth-value ‘Unknown’

Truth table for three-value logic

Does this query give all Movies in output?


Example: The Truth-value ‘Unknown’

What are the results of the following


query?

=> Movies tuples with non-NULL lengths


Ordering the Output

While retrieving data from the database,


you may need to specify the order in which
the rows are displayed.
In that case, the solution is to use the
ORDER BY clause
Ordering the Output

SELECT [ ALL | DISTINCT ]


    [ TOP n [ PERCENT ] ]
* | {column_name | expression [alias],…}
FROM table
[WHERE conditional expression]
[ORDER BY {expression1 [ASC | DESC], expression2 [ASC | DESC] …} ]

 If you use the ORDER BY clause, it must be the last clause of the SQL
statement.
 ORDER BY clause: Specifies one or many column on which to sort.
A sort column can be specified as a name or column alias, an expression,
or a nonnegative integer representing the position of the name, alias, or
expression in select list.
 Multiple sort columns can be specified. The sequence of the sort columns
in the ORDER BY clause defines the organization of the sorted result set.
 The results are first sorted on the first column; then the tuples having the same value on
the first column will be sorted on the second column, and so on.
Example: Ordering the output

 Sorting by column name

 Sorting by column alias

 Sorting by Multiple Columns

You might also like