Basic Structure SQL
Basic Structure SQL
1. INTRODUCTION
A language to specify queries for a relational database (in a
structured manner)
DECLARATIVE LANGUAGE :
The language which Specifies what to do, but not how to do.
Example:
User must find male students in a class. To do this with
procedural languages like C++, Java, etc., need to write code
that how to select those records. (Using control structures).
Multi-sets
Example: {A, A, B}
It is distinct from {A, B} but equivalent to {A, B, A}
• Integrity.
• View definition.
• Transaction control.
5
5. FROM CLAUSE
6
6. WHERE CLAUSE
7
A TYPICAL SQL QUERY HAS THE FORM
9
QUERIES
3. Fetch ID, Name and Salary fields from the CUSTOMERS table where
salary is greater than 2000
4. Fetch ID, Name and Salary fields from the CUSTOMERS table for a
customer with name ramesh.
10
SQL> CREATE TABLE CUSTOMERS( ID INT NOT
NULL, NAME VARCHAR (20) NOT NULL, AGE INT
NOT NULL, ADDRESS CHAR (25) , SALARY
DECIMAL (18, 2), PRIMARY KEY (ID) );
11
1. SELECT * FROM customers;
2. SELECT ID, NAME, SALARY FROM CUSTOMERS;
3. SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY > 2000;
4. SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE NAME = ‘ramesh';
12
BANKING ENTERPRISE - EXAMPLE
13
RELATIONS
14
15
DISTINCT KEYWORD
Fetch all the branches from the relation, see that you must
branch name only once
17
DISTINCT – TWO COLUMN
18
DISTINCT THREE COLUMNS
19
DISTINCT ALL COLUMNS
20
ALL KEYWORD
21
CLASS WORK
Display salaries of all customers without duplications.
22
SOLUTIONS
SELECT Distinct SALARY FROM CUSTOMERS;
23
ARITHMETIC EXPRESSIONS
select
loan-number,
branch-name,
amount * 100 24
from loan
CLASS WORK
1. Select all from customer where prebal + curbal is greater
than 5000.
2. Select all from customer where outstanding_amt' - 'payment
_ amt' is equal to the 'receive _ amt‘
2. SELECT *
FROM customer
WHERE(outstanding_amt-payment_amt)=receive_amt;
select loan-number
from loan
where branch-name = ’Perryridge’ and amount > 1200
27
CLASS WORK
Fetch ID, Name and Salary fields from the
CUSTOMERS table where salary is greater than 2000
AND age is less tan 25 years
28
SOLUTIONS
SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS
WHERE SALARY > 2000 AND age < 25;
29
BETWEEN CLAUSE
Find the loan number of those loans with loan amounts between $90,000 and $100,000
select loan-number
from loan 30
where amount between 90000 and 100000
PROBLEM
31
MULTIPLE RELATIONS
For all customers who have a loan from the bank, find their
names, loan numbers and loan amount.
select customer-name,
borrower.loan-number, amount
32
MULTIPLE RELATIONS WITH
MULTIPLE CONDITIONS
Find the customer names, loan numbers, and loan amounts for all
loans at the Perryridge branch
select customer-name,
borrower.loan-number, amount
34
SOLUTIONS
1.
SELECT * FROM agents
WHERE commission BETWEEN 1000 AND 2000;
2.
SELECT * FROM agents
WHERE commission NOT BETWEEN 1000 AND 2000;
3. SELECT *
FROM orders
WHERE ord_date NOT BETWEEN '15-Feb-08' AND '30-Jul-08';
35
UNIVERSITY SCHEMA – EXAMPLE 2
36
FIND QUESTIONS FOR THE FOLLOWING
1.
2.
SELECT *
FROM Faculty
WHERE FacNo = '543-21-0987';
37
3.
SELECT FacFirstName, FacLastName, FacSalary
FROM Faculty
WHERE FacSalary > 65000 AND FacRank = 'PROF';
4.
SELECT FacCity, FacState
FROM Faculty;
5.
SELECT DISTINCT FacCity, FacState
FROM Faculty;
38
CLASS WORK
39
employee
QUERIES
40
SOLUTIONS
41