DBMS Lecture Notes
DBMS Lecture Notes
DBMS Lecture Notes
commands.
SELECT Statement:
The SELECT statement is used to select data from a database. The data returned is stored
in a result table, called the result-set.
Syntax:
SELECT column1, column2, ...
FROM table_name;
Example:
LOAN:
- create table Loan(loan_number INTEGER PRIMARY KEY, amount INTEGER);
- INSERT INTO Loan VALUES(1001, 105321);
- INSERT INTO Loan VALUES(1057,100000);
- INSERT INTO Loan VALUES(1070, 87654);
- INSERT INTO Loan VALUES(1059, 287932);
- INSERT INTO Loan VALUES(1025,109065);
- Select * from Loan
CONCLUSION:
Viewing commands for table(like Select) have been learnt.
AIM: To study modifying commands(Update, Delete) and Execute queries using
these commands.
UPDATE Statement:
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
-
- select * from Payment Order by payment_date;
The SQL DELETE Statement
Syntax:
DELETE FROM table_name WHERE condition;
CONCLUSION:
Modifying commands for table(like Update, Delete) have been learnt.
AIM: To study aggregate functions (Count, Average, Sum, Min, Max) and Execute
queries using these commands.
The COUNT() function returns the number of rows that matches a specified criteria.
Syntax:
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
Example:
- SELECT COUNT(amount) from Loan;
Syntax:
SELECT AVG(column_name)
FROM table_name
WHERE condition;
Example:
Syntax:
SELECT SUM(column_name)
FROM table_name
WHERE condition;
Example:
- SELECT SUM(amount) from Loan;
The MAX() function returns the largest value of the selected column.
Syntax:
SELECT MAX(column_name)
FROM table_name
WHERE condition; MAX()
Example:
- select max(start_date) from Employee where dep_name = 'abc';
CONCLUSION:
Working with Aggregate functions (like Count, Average, Sum, Min, Max) have been
learnt.
AIM: To study grouping commands (Group by, Order by) and Execute queries using
these commands.
GROUP BY:
The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN,
SUM, AVG) to group the result-set by one or more columns.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Example:
The ORDER BY keyword is used to sort the result-set in ascending or descending order. The
ORDER BY keyword sorts the records in ascending order by default. To sort the records in
descending order, use the DESC keyword.
Syntax:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Example:
- select * from Loan order by loan_number;
CONCLUSION:
Grouping Commands (like group by, order by) have been learn
AIM: To study commands involving data constraints (Where, Having, And, Or, Not,
Between) and Execute queries using these commands.
WHERE Clause:
The WHERE clause is used to filter records. The WHERE clause is used to extract only
those records that fulfill a specified condition.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
- select * from Loan where loan_number = 1001;
HAVING Clause:
The HAVING clause was added to SQL because the WHERE keyword could not be
used with aggregate functions.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Example:
SELECT Branch_city, SUM(Assets) AS Assets FROM Branch GROUP BY Branch_city HAVING
The WHERE clause can be combined with AND, OR, and NOT operators. The AND and OR
operators are used to filter records based on more than one condition:
The AND operator displays a record if all the conditions separated by AND are TRUE.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
Example:
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
Example:
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
Example:
- select * from Payment where not payment_amount>50000;
BETWEEN Operator:
The BETWEEN operator selects values within a given range. The values can be
numbers, text, or dates. The BETWEEN operator is inclusive: begin and end values are
included.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Example:
- select * from Employee where emp_id between 2 and 5;
CONCLUSION:
Commands involving data constraints (Where, Having, And, Or, Not, Between) have
been learnt
AIM: To study aliasing and renaming(View, As) and Execute queries using these
commands.
CREATE VIEW:
A view contains rows and columns, just like a real table. The fields in a view are fields
from one or more real tables in the database. You can add SQL functions, WHERE, and
JOIN statements to a view and present the data as if the data were coming from one single
table.
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
create view low_payment as select payment_date,payment_amount from Payment where
payment_amount < 30000;
CONCLUSION:
Aliasing and renaming commands (like View, As) have been learnt
AIM: To study join commands and Execute their queries.
JOINS:
A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.
(INNER) JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Return all records from the left table, and the matched
records from the right table
RIGHT (OUTER) JOIN: Return all records from the right table, and the matched
records from the left table
FULL (OUTER) JOIN: Return all records when there is a match in either left or
right table
Example:
- SELECT C.Customer_id, C.Customer_name, A.Balance FROM Customer C
INNER JOIN Account A ON C.Customer_id = A.Account_id;
ACCOUNT
EMPLOYEE
UNION Operator:
The UNION operator is used to combine the result-set of two or more SELECT statements.
Each SELECT statement within UNION must have the same number of columns
The columns must also have similar data types
The columns in each SELECT statement must also be in the same order
Syntax:
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
Example:
- select emp_name from employee union select account_name from Account;
INTERSECT Operator:
The INTERSECT operator is used to combine two SELECT statements, but returns rows only
from the first SELECT statement that are identical to a row in the second SELECT statement.
This means INTERSECT returns only common rows returned by the two SELECT statements.
Syntax:
SELECT column_name(s) FROM table1
WHERE condition
INTERSECT
SELECT column_name(s) FROM table2
WHERE condition
Example:
- select emp_name from employee INTERSECT select account_name from Account;
MINUS Operator:
The MINUS operator is used to return all rows in the first SELECT statement that are not
returned by the second SELECT statement. Each SELECT statement will define a dataset. The
MINUS operator will retrieve all records from the first dataset and then remove from the results
all records from the second dataset.
Syntax:
SELECT column_name(s) FROM table1
WHERE condition
MINUS
SELECT column_name(s) FROM table2
WHERE condition
Example:
- select emp_name from employee minus select account_name from Account;
CONCLUSION:
Set Operation Commands (Union, Intersection, Minus) have been learnt.
AIM: To study commands for String Functions (ASCII, Lower, Upper, Length,
Inticap, Translate, Substr) and Execute queries using these commands.
ASCII Function:
The ASCII function returns the decimal representation in the database character set of the first character
of char. Example: ASCII('b') =98
Syntax:
select ASCII(col_name) from Table_Name
Example:
- select ASCII(emp_name) from employee;
Lower Function:
The LOWER function returns a string with all lower case characters. Example: LOWER('ORADEV') =
'oradev'
Syntax:
select LOWER(col_name) from Table_Name
Example:
- select LOWER(account_name) from Account;
-
-
Upper Function:
Transform a string to all upper case characters. Example: UPPER('oradev') = 'ORADEV'
Syntax:
select upper(col_name) from Table_Name
Example:
- select
Length Function:
The LENGTH functions returns the length of char. LENGTH calculates length using characters as
defined by the input character set. Example: length('oradev.com') = 10
Syntax:
select length(col_name) from Table_Name
Example:
- select length(amount) from loan;
Inticap Function:
Transform String to init cap Example: INITCAP('ORADEV') = 'Oradev'
Syntax:
select initcap(col_name) from Table_Name
Example:
- select initcap(emp_name) from employee
Substr Function:
Returns a substring. For more information see Oracle substring
Syntax:
select substr(col_name,st_idx, end_idx) from Table_Name
Example:
- select substr(emp_name,0,4) from employee;
CONCLUSION:
Commands for String Functions (ASCII, Lower, Upper, Length, Inticap, Translate,
Substr) were learnt.