0% found this document useful (0 votes)
4 views

TOPIC-10-SQL-RELATIONAL-OPERATION

Gives topic about SQO

Uploaded by

jemss1854
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

TOPIC-10-SQL-RELATIONAL-OPERATION

Gives topic about SQO

Uploaded by

jemss1854
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Mater Dei College

Tubigon, Bohol
2nd Semester, A.Y. 2024-2025
CC 105 – IM 101 (INFORMATION MANAGEMENT 1)

SQL QUERY STATEMENT (Retrieving Data From A Table)

RELATIONAL OPERATION
A relational operation is an operation that extracts the desired data from a table. There are
three basic relational operations.
Project---- extract the specified item from the table
Select-----extract the specified record from the table
Join-----extract data that combines two or more tables by means of a certain item with the same value

PROJECTION AND SELECTION


Below is an example showing the result of extracting data by projection and selection using a
SELECT statement on the table EMPLOYEE_TBL. We can also combine selection and projection.

EMPLOYEE_TBL Select name, department from employee_tbl;


Name Department Home country Age
Jimmy Sales Japan 28
Frank Human Resources USA 22 Name Department
Billy Sales France 35 Projection Jimmy Sales
Harry Sales Italy 43 (extracting Frank Human Resources
names and Billy Sales
Josh General Affairs Germany 48
departments)
Randy Human Resources USA 36 Harry Sales
Steve sales UK 31 Josh General Affairs
select * from employee_tbl where age >= 35; Randy Human Resources
Selection & Projection Selection( extracting rows where the age is 35 or above Steve sales
Name Department
Billy Sales Name Department Home country Age
Harry Sales Billy Sales France 35
Josh General affairs Harry Sales Italy 43
Randy Human resources Josh General affairs Germany 48
Select name, department Randy Human resources USA 36
From employee_tbl
Where age>= 35;

CLAUSES IN SQL STATEMENT:


Select –retrieves rows, columns, and derived values from one more tables.
From – one of the two most important clauses in SQL statement. This refers to the table where data is to be
extracted or retrieved.
Where – filtering unwanted rows from the result.
Order by – sort rows in ascending or descending order.
Group by – consolidates all rows that have the same value in a certain column.

Creating Column Aliases with AS


Column alias is an alternative name (identifier) that you specify to control how column headings are displayed
in a result. Use column aliases if column names are cryptic, hard to type, too long or too short.
➢ Must be specified in select clause of a select statement
➢ Follow a column name with the AS keyword and an alias enclosed in single or double quotes with the ff.
variations
o Omit the quotes if the alias is a single word that contains only letters, digits, or underscore.
o Use quotes if the alias contains spaces, punctuation or special characters
Ex. Select Cust_id as “customer id”, Lname as Lastname, address from customer;
Eliminating duplicate rows with DISTINCT
Columns often contain duplicate values, and it’s common to want a result tjat list each duplicate only
once.
Ex. Select DISTINCT address from customer;

Sorting rows with ORDER BY


Rows in query result are unordered. Use the order by clause to sort rows by a specified columns or
columns in ascending (asc) or descending (desc).
Ex. Select * from customer order by age asc;

Filtering rows with WHERE


Use the WHERE clause to filter unwanted rows from the result. WHERE clause- specifies a search
condition that comprises one or more conditions that need to be satisfied by the rows of a table. A condition,
or predicate, is a logical expression that evaluates to true, false or unknown. The unknown result arises from
nulls. Rows for which the condition is true are included in the result; rows for which the condition is false or
unknown are excluded.
SQL provides operators that express different types of conditions. Operators are symbols or keywords
that specify actions to be performed in values or other elements. You can combine and negate conditions by
using the logical operators AND, OR,NOT

Types of Condition and Their corresponding operators


Comparison – with the following operators =, <>, <, <=, >, >=
Pattern Matching – LIKE
Range Filtering – BETWEEN
List Filtering – IN
Null Test – IS NULL

Comparison
EXAMPLE
1. select * from employee_tbl where age >= 35;
2. Select name, department From employee_tbl Where age>= 35;

Pattern Matching
Use LIKE to retrieve rows based on partial information. Like is useful if you don’t know an exact value.
Like works with only characters strings, it uses pattern that values are matched against. A pattern is a quoted
string that contains the literals characters to match and any combination of wildcards. Wildcards are special
characters used to match parts of a value. You can negate like condition with not like, can combine like
conditions and other conditions with AND and OR.

Wildcards operators
% a percent sign matches any string of zero or more characters
_ an underscore matches any one character.

Ex. Select fname, lname from customer where fname like ‘%Ke’;
Select fname, lname from customer where fname not like ‘Ke%’;

Examples of % and _ patterns


PATTERN MATCHES
‘a%’ -matches a string value of length >= 1 that begins with a, including single letter A.
‘%s’ - matches a string value of length >= 1 that ends with s, including single letter s.
‘%in% -matches a string value of length >= 1 that contains in anywhere.
‘____’ -matches any four characters string value.
‘Qua__’ -matches any five characters string value that begins with Qua.
‘_re_’ -matches any four characters string value that has re as its second and third characters.
‘_re%’ -matches a string values of length >= 3 that begins with any character and has re as its
second and third characters.
‘%re_’ - matches a string value of length >=3 that has re as the second and third characters from its
end, and ends with any character.

Range Filtering with BETWEEN


Use between to determine whether a given value falls within a specified range.
Important characteristics of between conditions are:
➢ Between works with character strings, numbers, and datetimes.
➢ The between range contains a low value and a high value, separated by AND. The low value must be
less than of equal to the high value.
➢ Between is a convenient shorthand clause, you can replicate its behavior by using AND;
o Where test_column BETWEEN low_value and high_value
Is equivalent to
Where (test_column>= low_value) and (test_column<= high_value)
➢ Between specifies an inclusive range, in which the high value and low value are included in the
result/search. To specify an exclusisve range, which excludes endpoints, use > and < comparisons
operator instead of between operator.
➢ You can negate a between conditions with not between operator.
➢ You can combine BETWEEN conditions and other conditions with AND and OR.

List Filtering with IN


Use IN to determine whether a given value matches any value in a specified list.
➢ IN works with character strings, numbers, and datetimes
➢ The IN list is a parenthesized listing of one or more comma-separated values.
➢ IN is a convenient, shorthand clause. You can replicate its behavior by using OR:
Where test_columnIN(value1, value2, value3)
Is equivalent to
Where (test_column = value1) OR (test_column = value2) OR (test_column = value3)
➢ You can negate an IN condition with NOT IN.
➢ You can combine IN conditions and other conditions with AND and OR.

Testing for Nulls with IS NULL


Null represent missing or unknown values. This situation causes a problem: LIKE, BETWEEN, IN, and
other WHERE clause options can’t find nulls because unknown values don’t satisfy specific conditions. Nulls
match no value. IS NULL determines whether a given value is null.
➢ Works for columns of any data type
➢ You can negate an IS NULL condition with IS NOT NULL
➢ You can combine IS NULL conditions and other conditions with AND and OR.
EX. SELECT columns from table where test_column IS [NOT] NULL;

You might also like