Professional Documents
Culture Documents
LECTURE 4
Structure Query Language
SQL is a standard language for storing, manipulating and retrieving
data in databases
SQL commands are case insensitive.
SQL is non procedural query language.
SQL statements are divided into three category
DDL(Data Definition language)
Create, Alter, drop, truncate
1/8/2021
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
1/8/2021
Create table student ( Rollno varchar(20) primary key, Name
varchar(10), Dob date, Phone number(10));
1/8/2021
ALTER TABLE table_name ADD column_name datatype;
Restrictions on Alter:
We can’t decrease the size of attribute if data present.
We can’t add constraint if data available in attribute not satisfy the
condition of constraint.
1/8/2021
DROP TABLE table_name;
drop table student;
1/8/2021
Data Manipulation Language
1/8/2021
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE
condition;
UPDATE Customers SET ContactName = ‘rahul', City= ‘indore‘ WHERE
CustomerID = 1
Update student set phone = 9696969696;
1/8/2021
DELETE FROM table_name;
1/8/2021
SELECT * FROM table_name;
1/8/2021
Employee( Ename , Empno ,job ,salary ,deptno)
1/8/2021
Employee( Ename , Empno ,job ,salary ,deptno)
1/8/2021
Consider a database table T containing two columns X and Y each of type
integer. After the creation of the table, one record (X= 1, Y=l) is inserted in the
table. Let MX and MY denote the respective maximum values of X and Y among
all records in the table at any point in time. Using MX and MY, new records are
inserted in the table 128 times with X and Y values being MX+1, 2*MY+1
respectively. It may be noted that each time after the insertion, values of MX
and MY change. What will be the output of the following SQL query after the
steps mentioned above are carried out?
SELECT Y FROM T WHERE X=7;
(A) 127 (B) 255 (C) 129 (D) 257
1/8/2021
Creating Table with the help of another table.
1/8/2021
Insert into Table with the help of another table.
1/8/2021
RENAME table_name TO new_table_name;
Describe <table_name>
Describe student; (it describe the structure of table )
1/8/2021
SQL by default print duplicate data. If we want to element duplicity
we should use distinct keyword.
Example:
1/8/2021
Employee( ename , salary, empno ,job ,deptno, email, commision)
WAQ to find name of all employees whose name start with C and end
with T.
1/8/2021
Employee( ename , salary, empno ,job ,deptno, email, commision)
WAQ to find name of all employees whose name contain atleast 2 a’s.
WAQ to find name of all employees whose name start with C and at
third position B.
1/8/2021
Employee( ename , salary, empno ,job ,deptno, email, commision)
Wild characters :
1. /
2. %
3. _
1/8/2021
Symbol Description Example
Represents zero or bl% finds bl, black,
%
more characters blue, and blob
Represents a single h_t finds hot, hat, and
_
character hit
Represents any single
h[oa]t finds hot and
[] character within the
hat, but not hit
brackets
Represents any
h[^oa]t finds hit, but
^ character not in the
not hot and hat
brackets
c[a-b]t finds cat and
Represents a range of
- cbt
characters
1/8/2021
LIKE Operator Description
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
1/8/2021
The ORDER BY keyword sorts the records in ascending order by default.
To sort the records in descending order, use the DESC keyword.
1/8/2021
What is output of the following query?
1/8/2021
Aggregate functions or group valued function:
MIN(): The MIN() function returns the smallest value of the selected
column
MAX(): The MAX() function returns the largest value of the selected
column.
SUM(): The SUM() function returns the total sum of a numeric column.
AVG(): The AVG() function returns the average value of a numeric column.
COUNT(): The COUNT() function returns the number of rows that matches
a specified criterion
1/8/2021
Eno Ename Salary Deptno
1 Ankit 1000 10
2 Raj 2000 20
3 Ram 10
4 Zubin 3000 30
5 Rohit 1000 40
1/8/2021
GROUP BY:
1/8/2021
HAVING:
The HAVING clause was added to SQL because the WHERE keyword
could not be used with aggregate functions
1/8/2021
WAQ to find deptno with more then 5 employee
1/8/2021
Which of the following statements are TRUE about an SQL query?
P : An SQL query can contain a HAVING clause even if it does not have a GROUP
BY clause
Q : An SQL query can contain a HAVING clause only if it has GROUP BY clause
R : All attributes used in the GROUP BY clause must appear in the SELECT clause
S : Not all attributes used in the GROUP BY clause need to appear in the SELECT
clause
(A) P and R (B) P and S (C) Q and R (D) Q and S
1/8/2021
Sub Query
WAQ to find name of employee drawing the salary greater then the salary of
Ram.
1/8/2021
1/8/2021
1/8/2021
Correlated SubQuery
1/8/2021
Correlated subqueries are used for row-by-row processing. Each
subquery is executed once for every row of the outer query.
1/8/2021
The relation book (title, price) contains the titles and prices of
different books. Assuming that no two books have the same price,
what does the following SQL query list?
select title from book as B where (select count(*) from book as T
where T.price > B.price) < 5
(A) Titles of the four most expensive books
(B) Title of the fifth most inexpensive book
(C) Title of the fifth most expensive bookTitles of the five most
expensive books
(D) Titles of the five most expensive books
1/8/2021
SQL Constraints
1/8/2021
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
1/8/2021
CREATE TABLE Student(Rollno varchar(10) NOT NULL, Name
varchar(60), Age number(5));
1/8/2021
CREATE TABLE Student(Rollno varchar(10), Name varchar(60), Age number(5),
unique(ID));
1/8/2021