Springdales School Dhaula Kuan: Computer Science STD - Xii
Springdales School Dhaula Kuan: Computer Science STD - Xii
DHAULA KUAN
COMPUTER SCIENCE
STD.XII
SQL
Structured Query Language
SQL – Structured Query Language
➢ Is a language that enables you to create and operate
on relational databases
➢ It is the standard language used by almost all the
database s/w vendors.
➢ Pronounced as SEQUEL
➢ Original version was developed by IBM’s Almanden
Research Center
➢ Latest ISO standard of SQL was released in 2008 and
named as SQL:2008
SQL – features
➢ Allows creating/modifying a database’s structure
➢ Changing security settings for system
➢ Permitting users for working on databases or tables
➢ Querying database
➢ Inserting/modifying/deleting the database contents
MYSQL Elements
➢ Literals
➢ Data types
➢ Nulls
➢ Comments
Literals
➢ It means the fixed value or constant value. It may be of
character, numeric or date time type.
➢ Character and date/time literals are always in single
quotation marks whereas numeric literals must be
without single quotation marks
➢ For example – ‘Virat’, 12, 12.56, ‘04-20-2018’
➢ Date and time values are always in the format
YYYY-MM-DD HH:MI:SS
➢ Special character like quotes are always written be
preceding it back-slash(\). For example if we want to
store value as Tom’s Cat then it should be written as
Tom\’s Cat
Data Type
➢ Means the type of value and type of operation we
can perform on data. For example on numeric value
we can store numbers and perform all arithmetic
operations and so on.
➢ MySQL support three categories of data types:
➢ Numeric
➢ Date and time
➢ String types
Numeric Data Types
Data type Description
INT Numbers without decimal. Store up to 11 digits. -2147483648 to 2147483647
Example:-
EXAMPLE:
DESC EMPLOYEE;
DESC STUDENT;
MySQL allows us to get the structure of table
like list of columns, data type, size and key
information of table using DESC / DESCRIBE
command
Example
INSERTING RECORDS IN TABLE
Syntax:-
Insert into tablename values(value1,value2,…)
Note:-
1) char, varchar and date value must be in single
quotes
2) Values must be passed in the order of their column
3) Date values are passed in the format
yyyy-mm-dd (in mysql)
INSERTING RECORDS IN TABLE
Syntax:-
Syntax:-
Select * / columnnames FROM tablename
[ where condition ];
SELECTING RECORD
From the above table we can observe that salary of Shaban is NULL i.e.
not assigned, Now if we want 0 or “not assigned” for the salary
information of shaban, we have to use IFNULL()
Select empno,name,IFNULL(Salary,”not assigned”) from emp;
Empno Name
2 Sunny
WHERE clause
AND(&&) means both conditions must be true, OR(||) means
any condition must be true to produce output. NOT(!) will do
the reverse checking.
Select * from emp where salary between 18000 and 30000; Select
Select * from emp where salary NOT between 25000 and 35000;
BETWEEN
Select * from emp where salary between 18000 and 30000;
OR
Select * from emp where salary >= 18000 AND salary <= 30000;
OR
NOTE :- when we are using GROUP BY we can use only aggregate function and the
column on which we are grouping in the SELECT list because they will form a group other
than any column will gives you an error because they will be not the part of the group.
For e.g.
SELECT ENAME,JOB,SUM(SAL) FROM EMP GROUP BY JOB;
Error -> because Ename is not a group expression
HAVING with GROUP BY
• If we want to filter or restrict some rows from the output produced by
GROUP BY then we use HAVING clause. It is used to put condition of group of
rows. With having clause we can use aggregate functions also.
• WHERE is used before the GROUP BY. With WHERE we cannot use aggregate
function.
• E.g.
• SELECT DEPT,AVG(SAL) FROM EMP GROUP BY DEPT HAVING
DEPT IN (‘HR’,’SALES’)
DATE() Return date part from date- time Select date(‘2018-08-15 12:30’);
expression Output: 2018-08-15
MONTH() Return month from date Select month(‘2018-08-15’); Output: 08
YEAR() Return year from date Select year(‘2018-08-15’); Output: 2018
DAYNAME() Return weekday name Select dayname(‘2018-12-04’);
Output: Tuesday
DAYOFMONTH() Return value from 1-31 Select dayofmonth(‘2018-08-15’)
Output: 15
DAYOFWEEK() Return weekday index, for Select dayofweek(‘2018-12-04’);
Sunday-1, Monday-2, .. Output: 3
DAYOFYEAR() Return value from 1-366 Select dayofyear(‘2018-02-10’)
Output: 41
Date and Time Function
Function Description Example
NOW() Return both current date Select now();
and time at which the
function executes
SYSDATE() Return both current date Select sysdate()
and time
TABLE: VEHICLE
VCODE VEHICLETYPE PERKM
V05 SUV 30
V04 CAR 18
TABLE: TRAVEL
CNO CNAME TRAVELDATE KM VCODE NOP