0% found this document useful (0 votes)
17 views

SQL Practical

Structured Query Language (SQL) is a standard language used to manage data in relational database management systems (RDBMS). SQL is used to create and modify database structures like tables, indexes, and constraints. It is also used to retrieve and manipulate data. Common SQL commands include SELECT to query data, INSERT to add new data, UPDATE to modify existing data, and DELETE to remove data. SQL defines several data types including text, number, date, and logical data types.

Uploaded by

metaverse714
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

SQL Practical

Structured Query Language (SQL) is a standard language used to manage data in relational database management systems (RDBMS). SQL is used to create and modify database structures like tables, indexes, and constraints. It is also used to retrieve and manipulate data. Common SQL commands include SELECT to query data, INSERT to add new data, UPDATE to modify existing data, and DELETE to remove data. SQL defines several data types including text, number, date, and logical data types.

Uploaded by

metaverse714
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Structured Query Language

(SQL)

Prepared By : Bijay Kushwaha


Structured Query Language(SQL)

Structured Query Language(SQL) is a


database query language designed for the
retrieval and management of data in a
relational database management system
(RDBMS). All the RDBMS like MySQL,
Oracle, MS Access, Informix, Sybase ,DB2
and SQL Server use SQL as their standard
database language.

Prepared By : Bijay Kushwaha


Data Types in SQL

 SQL Datatype is used to define the values


that a column can contain.
 Every column is required to have a name and
data type in the database table.

Prepared By : Bijay Kushwaha


Text Data Type:
Data type Description
CHAR Holds fixed length string. Up to 255 Characters
VARCHAR Holds variable length string. Up to 255 Characters

TINYTEXT Holds a string with a maximum length of 255


characters
TEXT Holds a string with a maximum length of 65,535 bytes
BLOB(binary large Holds upto 65,535 bytes of data
objects)
MEDIUMTEXT Holds a string with a maximum length of 16,777,215
characters
MEDIUMBLOB Holds up to 16,777,215 bytes of data.
LONGTEXT Holds a string with a maximum length of
4,294,967,295 characters
LONGBLOB Holds up to 4,294,967,295 bytes of data
ENUM(x,y,z etc) Up to 65535 values in an ENUM
SET Contain up to 64 list items & can store more than one
Prepared
choice. By : Bijay Kushwaha
Number Data Type:
Data type Description Storage size
TINYINT Allows whole numbers from 0 to 255 1 byte
SMALLINT Allows whole numbers between -32,768 and 32,768 2 bytes

MEDIUM Allows whole numbers between -83,88,608 and


83,88,608
INT Allows whole numbers between -2,14,483,648 and 4 bytes
2,14,483,648

BIGINT Allows whole numbers between - 8 bytes


9223372036854775808 and 9223372036854775807

FLOAT A small number with a floating decimal point. 4 or 8 bytes


DOUBLE A large number with a floating decimal point.

DECIMAL Allow for a fixed decimal point.

Prepared By : Bijay Kushwaha


Date Data Type:
Data type Description Storage
DATE() Stores a date only. 3 bytes
DATETIME() Stores combination of date and time 4 bytes

TIMESTAMP() TIMESTAMP values are stored as


number of second.
TIME() Stores a time only 3-5 bytes
YEAR() Stores rear in two-digit or four digit
format

Prepared By : Bijay Kushwaha


SQL command
 SQL commands are instructions. It is used to
communicate with the database. It is also used to
perform specific tasks, functions, and queries of
data.
 SQL can perform various tasks like create a table,
add data to tables, drop the table, modify the table,
set permission for users.

Prepared By : Bijay Kushwaha


Types of Structured Query Language(SQL)
1. DDL (Data Definition Language)
2. DML (Data Manipulation Language)
3. DCL (Data Control Language)
4. TCL (Transaction Control Language)

Prepared By : Bijay Kushwaha


(I) Data Definition Language(DDL)
DDL stands for data definition language. DDL is used by the
database designers and programmers to specify the content and
structure of the database. It is used to create tables, indexes,
constraints etc. in the database.
Some tasks that come under DDL are:
➢Create:- It is used to create objects in the database.
➢Alter:- It is used to alter the structure of the database.
➢Drop:- It is used to delete objects from the database.
➢Truncate:- It is used to delete table in database instance.
➢Rename:- It is used to rename an object.
➢Comment:- It is used to comment on the data dictionary.

Prepared By : Bijay Kushwaha


CREATE Statement
CREATE Statement is used to create tables and
databases, views, sequences indexes etc.
To Create Database:
Syntax :
CREATE DATABASE database_name;
Example:
CREATE DATABASE Bijay;

Prepared By : Bijay Kushwaha


DROP Statement:
DROP statement allows us to remove an existing database, table, views from database.
Syntax :
DROP DATABASE database_name;
Example:
DROP DATABASE SHYAM;
Syntax :
DROP TABLE table_name;
Example: DROP TABLE Student;
DROP DATABASE SHYAM;

Prepared By : Bijay Kushwaha


SQL USE Database
In SQL database, you need to select a database first before
executing any query on table, view etc. To do so, we use
following query:
Syntax:-
USE DATABASE database_name;
Example:-
USE SHYAM;

Prepared By : Bijay Kushwaha


CREATE TABLE Statement:-

The CREATE TABLE statement is used to create a


table in a database.

Syntax:
CREATE TABLE table_name
(
field 1 data_type,
field 2 data_type,
field 3 data_type,
……………………..
field n data_type
);
Prepared By : Bijay Kushwaha
Example:
CREATE TABLE Student
(
Roll Int Primary Key,
Name Varchar(40) Not Null,
Address Varchar(50),
Class Int
);
OUTPUT
Roll Name Address Class

Prepared By : Bijay Kushwaha


ALTER Statement:-
ALTER statement database is used to change the
structure of database tables, views or other objects. i.e. It
is used to add, delete or modify columns in an existing
table.
Adding new column:
Syntax:
ALTER TABLE table_name
ADD column_name datatype;
Example:
ALTER TABLE Student OUTPUT
ADD Phone Int; Roll Name Address Class Phone

Prepared By : Bijay Kushwaha


To remove an existing column

Syntax:
ALTER TABLE table_name
DROP (column_name);
Example:
ALTER TABLE Student
DROP Phone;

Prepared By : Bijay Kushwaha


TRUNCATE TABLE Statement
It is used to delete all the rows from the table and free
the space containing the table.
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE Student;

Prepared By : Bijay Kushwaha


Data Manipulation Language(DML):-
DML stands for Data Manipulation Language. It is used
for accessing and manipulating data in a database.
Database language that enables insert, delete, update
and retrieve data from database is called data
manipulation language(DML). Query language is only a
part of DML.
Some important DML statements are:
o INSERT INTO – Inserts new data into a database
o SELECT – Extracts data from a database
o UPDATE – Updates data in a database
o DELETE – Deletes data from a database

Prepared By : Bijay Kushwaha


INSERT INTO Statement:-
The INSERT INTO statement of SQL is used to insert a new
row in a table. There are two ways of using INSERT INTO
statement for inserting rows:
• Only values
• Column names and values both
Syntax:
INSERT INTO table_name VALUES(value1, value2,value3,……….);
Example:
INSERT INTO Student
VALUES(1,’Ram Sharma','Birgunj’,12);

INSERT INTO Student


VALUES(2,’Shyam Shrestha’,’Kathmandu’,12);
Roll Name Address Class
1 Ram Sharma Birgunj 12
2 Prepared By : Bijay Kushwaha
Shyam Shrestha Kathmandu 12
Syntax:
INSERT INTO table_name (col1, col2, col3,....)
VALUES (value1, value2, value3, ....);

Example:
INSERT INTO Student (Roll, Name, Address, Class)
VALUES(1,’Ram Sharma','Birgunj’,12);

INSERT INTO Student (Roll, Name, Address, Class)


VALUES(2,’Shyam Shrestha’,’Kathmandu’,12);
OUTPUT:

Roll Name Address Class


1 Ram Sharma Birgunj 12
2 Shyam Shrestha Kathmandu 12

Prepared By : Bijay Kushwaha


SQL SELECT STATEMENT:-
The select statement is used to select (fetch) data
from a database.
Syntax:
SELECT column_name(s) FROM table_name;
and
SELECT * FROM table_name;

STUDENT
Roll Fname Lname Address City
1 Ram Sharma Adarshnagar Birgunj
2 Shyam Yadav Ranighat Birgunj
3 Hari Shrestha Bagbajar Kathmandu
4 Sita Sharma Pulchock Lalitpur

Prepared By : Bijay Kushwaha


Example:
SELECT FirstName, LastName from Student;
OUTPUT:

SELECT * FROM Student;

OUTPUT:

Roll Fname Lname Address City


1 Ram Sharma Adarshnagar Birgunj
2 Shyam Yadav Ranighat Birgunj
3 Hari Shrestha Bagbajar Kathmandu
4 Sita Sharma Pulchock Lalitpur

Prepared By : Bijay Kushwaha


SELECT DISTINCT STATEMENT:-
The DISTINCT keyword can be used to return only
distinct (different) values.
Syntax:-
SELECT DISTINCT column_name(s) FROM
table_name;
Example:
SELECT DISTINCT City FROM Student;

OUTPUT:

Prepared By : Bijay Kushwaha


WHERE Clause:-
The WHERE clause is used to extract only those
records that fulfill a specified criteria.
Syntax:-
SELECT column_name(s)
FROM table_name
WHERE column_name operator values
Example:
SELECT *FROM Student
WHERE City='Birgunj' ; OUTPUT:

Prepared By : Bijay Kushwaha


WHERE Clause Operators

Prepared By : Bijay Kushwaha


SQL AND Operator:-
The AND Operator displays a record if both the first condition
and the second condition is true.
Example:
SELECT * FROM Student OUTPUT:
WHERE FName='Ram'
AND LName='Sharma';

SQL OR Operator:-The OR Operator displays a record if


either the first condition or the second condition is true.
Example:
SELECT * FROM STUDENT OUTPUT:
WHERE FName='Ram'
OR FName='Sita';

Prepared By : Bijay Kushwaha


COMBINING AND & OR

Example:
SELECT * FROM STUDENT WHERE
LName='Sharma'
AND ( FName='Ram' OR FName=' Sita');

OUTPUT:

Prepared By : Bijay Kushwaha


SQL ORDER BY Keyword :-

The ORDER BY keyword is used to sort the result-set by a


specified column , ascending order or descending order.
Syntax:-
SELECT column_name(s)
FROM table_name
ORDERED BY column_name(s) ASC/DESC;
OUTPUT:
Example:-
SELECT * FROM STUDENT
ORDER BY FName ASC;

Example:-
SELECT * FROM STUDENT
ORDER BY FName DESC;

Prepared By : Bijay Kushwaha


LIKE Operator:-
The LIKE Operator is used to search for a specified pattern
in a column.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern
OUTPUT:

Example:
SELECT * FROM STUDENT
WHERE city LIKE 'B%' ;

The % sign can be used to define wildcard(missing letters


in the pattern) both before & after the pattern.

Prepared By : Bijay Kushwaha


If we want to select the student living in a city that ends with
‘r' from the ''STUDENT' table
OUTPUT:
Example:
SELECT * FROM STUDENT
WHERE city LIKE '%r' ;

If you want to select the persons with a FName that starts


with any character, followed by "ari" from the STUDENT
table
Example:
SELECT * FROM STUDENT OUTPUT:

WHERE FName LIKE '_ari';

Prepared By : Bijay Kushwaha


If you want to select the persons with a FName that
starts with "R"or "S" from the STUDENT table
Example:-
SELECT * FROM STUDENT
WHERE FName LIKE '[RS]%';

OUTPUT:

Prepared By : Bijay Kushwaha


IN Operator:-
The IN Operator allows you to specify multiple values in
WHERE Clause.
Syntax:-
SELECT column_name(s)
FROM table_name
WHERE column_name IN (values1, values2,……….)
Example:
SELECT * FROM STUDENT
WHERE FName IN('Shyam','Sita','Hari'); OUTPUT:

Prepared By : Bijay Kushwaha


UPDATE Statement:-
The UPDATE Statement is used to update existing records
in a table.
Syntax:
UPDATE table_name
Set column1=value WHERE
some_column=some_value;

Student
Roll Name Address Class Phone

1 Ram Sharma Birgunj 12

2 Shyam Shrestha Kathmand 12


u

Prepared By : Bijay Kushwaha


Example:
UPDATE Student
SET Phone=9849903722 where Roll=1;

OUTPUT:
Roll Name Address Class Phone

1 Ram Sharma Birgunj 12 9849903722

2 Shyam Shrestha Kathmand 12


u

Prepared By : Bijay Kushwaha


DELETE Statement:-
DELETE Statement is used to delete an existing records or
rows from a table. Where clause can be used to delete
the selected rows, otherwise all the records will be
deleted.
Syntax:
DELETE FROM table_name WHERE Condition;
Example 1: Delete record of student whose Roll=1.
DELETE FROM Student
WHERE Roll=1;
2 To delete all records
Syntax:
DELETE FROM name _of_table;
Example:
DELETE FROM Student;

Prepared By : Bijay Kushwaha

You might also like