Relational Language
Relational Language
Relational Language
Query
Language
Working with DATES
SYSDATE is a date function that returns the
current database server date.
E.g
SELECT sysdate
FROM dual
Where dual is a dummy table
E.g
SELECT 2*2
FROM dual
Arithmetic Operators
There are four arithmetic operators and their order of
precedence is as follows only:
1. Multiply (*)
2. Division (/)
3. Addition (+)
4. Subtraction (-)
E.g
Select ename, salary, salary+100
From emp;
Concatenation Operator
E.g
Select Fname||Lname
From emp;
Comparison Operators
Literal Character String
E.g
Select Lname,salary Lower Limit Upper Limit
From emp
Where salary BETWEEN 25000 AND 35000
IN Condition
E.g
Select ename,sal,empno,deptno
From emp
Where ename IN (‘Ramos’,’Clark’,Pramada’,’Aruna’)
Where ename NOT IN (‘Ramos’,’Clark’,Pramada’,’Aruna’)
LIKE Condition
LIKE condition is used to perform the wildcard search
of valid search string values. Here % represents any
sequence of zero or more characters. – represents any
single character.
E.g
SELECT Lname,hire_date
FROM emp
WHERE hire_date LIKE ‘%95’;
E.g
SELECT empno, LOWER(lname), deptno
FROM emp
WHERE INITCAP(lname) = ‘wills’
Functions
1. AVG – SELECT AVG(sell_price) (Average)
2. MIN – SELECT MIN(bal_due) (Minimum
Balance)
3. COUNT(*) – SELECT COUNT(*) (No. of
rows in the table)
4. MAX – SELECT MAX(bal_due) (Maximum
Balance)
5. SUM – SELECT SUM(sal) (Total salary of all
employees)
6. ABS – SELECT ABS(n) (Absolute)
Functions
7. POWER – SELECT POWER(3,2) (Raised)
8. ROUND – SELECT ROUND(15.19,1) (Round)
9. SQRT – SELECT SQRT(25) (Square Root)
10. SUBSTR- SELECT SUBSTR(‘SECURE’,3,4)
(Substring)
11. LENGTH – SELECT LENGTH(‘ELEPHANT’)
(Length of the string)
12. LTRIM – SELECT LTRIM(‘NISHA’,’N’) (Removes
characters from the left of char with initial characters)
13. RTRIM – SELECT RTRIM(‘SUNILA’,’A’)
(Returns char removed after the last character not in
the set.)
Sorting results
• ORDER BY - sorts the final result rows in ascending or
descending order
• GROUP BY - groups rows in an intermediate results
table where the values in those rows are the same for
one or more columns
• HAVING - can only be used following GROUP BY
and acts as a secondary WHERE clause, returning only
those groups which meet a specified condition.It
applies on group functions only.
ORDER BY Clause
It sorts rows with the ORDER BY Clause
ASC : Ascending order (by default)
DESC : Descending order
The ORDER BY clause comes last in the SELECT
statement
E.g
SELECT lname,job_id,hire_date SELECT lname,job_id,hire_date
FROM emp FROM emp
ORDER BY hire_date ORDER BY hire_date DESC
ORDER BY Clause
E.g
SELECT prod_no, sum(qty_ordered)
FROM sales_order
GROUP BY prod_no
GROUP BY
• e.g. count the number of customers with
addresses in each state to which we deliver:
• Make sure that total column length does not exceed 8060 bytes
– SQL 2K gives a warning upon such table creation; error upon 8060+ insert
– SQL 2005 handles 8060+ tables with variable length columns
• At a price of performance impact
CREATE TABLE dbo.utbPhoneBook(
FirstName VARCHAR(128),
LastName VARCHAR(128),
PhoneNumber VARCHAR(32))
ON [PRIMARY]
Best Practices for Data Definitions -
Columns
• Naming convention
– Column names should be standardized, self-explanatory
(first_column, second_column, …
last_column)
DELETE
FROM Students S
WHERE S.name = ‘Smith’
Powerful variants of these commands are available;
more later!
Example using “insert”
insert into CD_MASTER values (101, ‘Fields of
Gold’, ‘Sting’, ‘Rock’);
update <table_name>
where <condition>;
Deleting Data
where <SQL_condition>;
Altering table definitions
alter table <table_name>
(<column specification[s]>);
Deleting tables
[cascade constraints];
Querying Multiple Relations
• What does the following query compute?
SELECT S.name, E.cid
FROM Students S, Enrolled E
WHERE S.sid=E.sid AND E.grade='A'
S.name E.cid
we get:
Smith Topology112
Basic Table Level Operations
• Creation: Using the create command
• Populating tables: Entering values into the table using
the insert command
• Modifying data: Modifying data in the tables using the
update command
• Deleting data: Deleting data from tables using the delete
command
• Altering tables: Using the alter command
• Deleting tables: Deleting tables using the drop command
SQL Overview
• CREATE TABLE <name> ( <field> <domain>, … )
• UPDATE <name>
SET <field name> = <value>
WHERE <condition>
• SELECT <fields>
FROM <name>
WHERE <condition>
Semantics of a Query
• Is used for queries on single or multiple tables. It has the following clauses:
• SELECT - Lists the columns (and expressions involving columns) from base tables
or views to be projected into the table that will be the result of the command
• FROM - Identifies the table(s) or view(s) from which columns will be chosen to
appear in the result table, and includes the tables or views needed to join tables to
process the query
• WHERE - Includes the conditions for row selection within a single table or view,
and the conditions between tables or views for joining.
• .
SELECT Example
• Find products with standard price less than $275
ENROL(studno,courseno,labmark,exammark)
COURSE(courseno,subject,equip)
STAFF(lecturer,roomno,appraiser)
TEACH(courseno,lecturer)
YEAR(yearno,yeartutor)
Assignment 2
Assignment 3
Q: What relationship or relationship set can be drawn among these
entities? Label the attributes (if u find any appropirate for that
relationship) also. Write the Schema for it. Specifying the Keys
related to it (if any).
Exercise
title
name
Movies Stars
year
address
Studios
name address
Stars-in Stars
Movies
name
length
filmType
Owns
Studios
address
Find the loan number for each loan of an amount greater than
$1200
loan-number (amount > 1200 (loan))
Example Queries
• Find the names of all customers who have a loan, an
account, or both, from the bank
customer-name (borrower) customer-name (depositor)
account at bank.
Query 1
customer-name(branch-name = “Perryridge” (
borrower.loan-number = loan.loan-number(borrower x loan)))
Query 2
customer-name(loan.loan-number = borrower.loan-number(
(branch-name = “Perryridge”(loan)) x borrower))
Example Queries
Find the largest account balance
• Rename account relation as d
balance
• The query(account)
is: - account.balance