0% found this document useful (0 votes)
67 views49 pages

RDBMS

This document provides an overview of SQL (Structured Query Language). It discusses the different types of SQL languages including DDL, DML, and DCL. It describes common SQL commands like CREATE, SELECT, INSERT, UPDATE, DELETE. It also covers SQL functions, joins, grouping, aggregation and other important SQL concepts. The document is intended as a guide to learning the basics of the SQL language.

Uploaded by

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

RDBMS

This document provides an overview of SQL (Structured Query Language). It discusses the different types of SQL languages including DDL, DML, and DCL. It describes common SQL commands like CREATE, SELECT, INSERT, UPDATE, DELETE. It also covers SQL functions, joins, grouping, aggregation and other important SQL concepts. The document is intended as a guide to learning the basics of the SQL language.

Uploaded by

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

SQL

Rameswara Reddy.K.V
DDL (Data Definition Language)

 CREATE - to create objects in the database.


 ALTER - alters the structure of the databas.
 DROP - delete objects from the database.
 TRUNCATE - remove all records from a table,
including all spaces allocated for the records are
removed.
 COMMENT - add comments to the data dictionary.
DML(Data Manipulation Language)

 SELECT - retrieve data from the a database.


 INSERT - insert data into a table.
 UPDATE - updates existing data within a table.
DELETE - deletes all records from a table, the space
for the records remain.
DCL (Data Control Language )

 COMMIT - save work done.


 ROLLBACK - restore database to original since the
last COMMIT
GRANT - gives user's access privileges to database.
REVOKE - withdraw access privileges given with the
GRANT command .
SQL - CREATE TABLE
create command syntax:
create table @table_name(@col_name1 data_type
constraint @constraint_name primary key,
@col_name2 data_type, … @col_namen data_type);
Ex:
Create table student(sid number, sname varchar2(30),
cgpa number(4,2));
Description of the table
desc @table_name;
Ex: desc student;
Alter Command – to alter the columns
alter table @table_name add @new_col_name1
data_type;

Ex: alter table student add grade varchar2(3);

alter table @table_name drop column


@col_name1;

Ex: alter table student drop column grade;


alter table @table_name modify @col_name1
new_data_type;

Ex: Alter table student modify grade char;

alter table @table_name modify @col_name1


existing_data_type(new_size);

Ex: Alter table student modify grade char(4);

alter table @table_name rename column


existing_col_name to new_column_name;

Ex: alter table student rename column grade to grades;


Truncate and Drop
truncate table @table_name;

Ex: truncate table student;

drop table @table_name;

Ex: drop table student;


DML
insert command:

insert into @table_name


values('&@col_name1(varchar2/char/varchar/date)',
&@col_name2(number));

Ex: insert into student values(&id, ’&name’, ‘&branch’ ,


&cgpa);
Select Command
select * from @table_name;
* -> all rows and all columns of a table

Ex: select * from student;

select @col_name1,@col_name2,...,@col_namen from


@table_name;
Ex: select id, name from student;

select @col_name1,... from @table_name where


<condition>;
Ex: select id, name, cgpa from student where id=123;
Delete command
delete from @table_name;
used to delete all rows present in a table
temporarily and you are able to recover it by
using rollback command
Ex: delete from student;

delete from @table_name where


<condition>;
used to delete selective rows based on the
condition
Ex: delete from student where id=123;
Update command:
update @table_name set
@col_name=new_value [where
<condition>];

Ex: update student set cgpa=79.23


where id=123;
DCL commands
Commit

Ex: commit;

Grant

Ex: Grant dba to username;

Revoke

Ex: revoke dba to username;


Roll back

Ex: rollback student;


Aggregate functions
Min

Ex: select Min(marks) from student;

Max

Ex: select Max(marks) from student;


Aggregate functions
Avg

Ex: select avg(marks) from student;

sum

Ex: select sum(marks) from student;


Aggregate functions
count

Ex: select count(marks) from student;

abs

Ex: select abs(-15) from dual;


Aggregate functions
power

Ex: select power(2,3) from dual;

sqrt

Ex: select sqrt(25) from dual;


Aggregate functions
lower

Ex: select lower(‘GPREC’) from dual;

upper

Ex: select upper(‘gprec’) from dual;


Aggregate functions
initcap

Ex: select initcap(‘hello gPREC world’) from dual;

length

Ex: select length(‘gprec’) from dual;


Aggregate functions
substr

Ex: select substr(‘hello gPREC world’,2) from dual;


Ex: select substr(‘hello gPREC world’, 2,5) from dual;

ltrim

Ex: select ltrim(‘xxxXgprec’, ‘x’) from dual;


Pattern Matching
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';

SELECT * FROM Customers
WHERE CustomerName LIKE '%a';

SELECT * FROM Customers
WHERE CustomerName LIKE '%or%';
SELECT * FROM Customers
WHERE CustomerName LIKE '_r%';

SELECT * FROM Customers
WHERE CustomerName LIKE 'a_ _%';

SELECT * FROM Customers
WHERE ContactName LIKE 'a%o';

SELECT * FROM Customers
WHERE CustomerName NOT LIKE 'a%';
In operator
The SQL IN Operator
The IN operator allows you to specify multiple values
in a WHERE clause.
The IN operator is a shorthand for multiple OR
conditions.
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
IN
Ex: SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');

Ex: SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France', 'UK');

Ex: SELECT * FROM Customers
WHERE Country IN (SELECT Country FROM Supplie
rs);
Range Search:
Between & Not Between
The SQL 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. 
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Ex: select empno,ename,job from emp where sal
between 1000 and 2000;

Ex: select empno,ename,job from emp where sal not


between 1000 and 2000;
Order by
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.
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Order by
SELECT * FROM Customers
ORDER BY Country;

SELECT * FROM Customers
ORDER BY Country DESC;

SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
Group by
The GROUP BY statement groups rows that have the
same values into summary rows, like "find the number of
customers in each country".
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.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;
SQL Joins

A JOIN clause is used to combine rows from two or more tables, based
on a related column between them.
Different Types of SQL JOINs
Here are the different types of the JOINs in SQL:
(INNER) JOIN: Returns records that have matching values in both
tables
LEFT (OUTER) JOIN: Returns all records from the left table, and the
matched records from the right table
RIGHT (OUTER) JOIN: Returns all records from the right table, and
the matched records from the left table
FULL (OUTER) JOIN: Returns all records when there is a match in
either left or right table
      
INNER JOIN

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

Ex: SELECT Orders.OrderID,
Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID =
Customers.CustomerID;
LEFT JOIN

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

Ex: SELECT Customers.CustomerName, Orders.OrderID


FROM Customers
LEFT JOIN Orders ON Customers.CustomerID =
Orders.CustomerID
ORDER BY Customers.CustomerName;
Right Join
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

Ex: SELECT Orders.OrderID, Employees.LastName,


Employees.FirstName
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID =
Employees.EmployeeID
ORDER BY Orders.OrderID;
Full Join
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

Ex: SELECT Customers.CustomerName, Orders.OrderID


FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=
Orders.CustomerID
ORDER BY Customers.CustomerName;
Natural Join
Joins two tables information.

Ex: select * from emp natural join dept;


Union
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
The UNION operator selects only distinct values by
default. To allow duplicate values, use UNION ALL:
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
Ex: SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
Union All
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;

Ex: SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;
Intersect
The INTERSECT clause in SQL is used to combine
two SELECT statements but the dataset returned by
the INTERSECT statement will be the intersection of
the data-sets of the two SELECT statements.
In simple words, the INTERSECT statement will
return only those rows which will be common to both
of the SELECT statements.
SELECT column1 , column2 .... FROM table_names WHERE
condition INTERSECT SELECT column1 , column2 .... FROM
table_names WHERE condition

Ex: select deptno from emp intersect select deptno from


dept order by deptno;

Ex:SELECT ID, NAME, Amount, Date FROM Customers LEFT


JOIN Orders ON Customers.ID = Orders.Customer_id
INTERSECT SELECT ID, NAME, Amount, Date FROM
Customers RIGHT JOIN Orders ON Customers.ID =
Orders.Customer_id;
PRIMARY KEY

The PRIMARY KEY constraint uniquely identifies each record in a


table.
Primary keys must contain UNIQUE values, and cannot contain
NULL values.
A table can have only ONE primary key; and in the table, this
primary key can consist of single or multiple columns (fields).
CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    PRIMARY KEY (ID)
);
Foreign Key
A FOREIGN KEY is a key used to link two tables
together.
A FOREIGN KEY is a field (or collection of fields) in
one table that refers to the PRIMARY KEY in another
table.
The table containing the foreign key is called the child
table, and the table containing the candidate key is
called the referenced or parent table.
Ex: CREATE TABLE Orders (
    OrderID int NOT NULL,
    OrderNumber int NOT NULL,
    PersonID int,
    PRIMARY KEY (OrderID),
    FOREIGN KEY (PersonID) REFERENCES Persons(P
ersonID)
);
https://
www.tutorialrepublic.com/co
delab.php?topic=sql&file=sel
ect-all

You might also like