0% found this document useful (0 votes)
2 views13 pages

SQL Lab Prerequisite Basic of Sql and query

SQL, or Structured Query Language, is a standard language for managing and manipulating databases, allowing users to create, alter, and query data. SQL commands are categorized into DDL, DML, DQL, DCL, and TCL, each serving different functionalities such as defining database structures, manipulating data, querying data, controlling access, and managing transactions. Key commands include CREATE, INSERT, SELECT, GRANT, and COMMIT, which facilitate various operations within relational database management systems.

Uploaded by

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

SQL Lab Prerequisite Basic of Sql and query

SQL, or Structured Query Language, is a standard language for managing and manipulating databases, allowing users to create, alter, and query data. SQL commands are categorized into DDL, DML, DQL, DCL, and TCL, each serving different functionalities such as defining database structures, manipulating data, querying data, controlling access, and managing transactions. Key commands include CREATE, INSERT, SELECT, GRANT, and COMMIT, which facilitate various operations within relational database management systems.

Uploaded by

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

SQL

SQL stands for Structured Query Language. SQL is used to create, remove, alter the
database and database objects
in a database management system and to store, retrieve, update the data in a database.
SQL is a standard language
for creating, accessing, manipulating database management system. SQL works for all
modern relational database
management systems, like SQL Server, Oracle, MySQL, etc.

Different types of SQL commands

SQL commands can be categorized into five categories based on their functionality

Different types of SQL commands

SQL commands can be categorized into five categories based on their functionality

DDL (Data Definition language)


A Data Definition Language (DDL) statement is used to define the database structure
or schema.
Aim: To study and execute the DDL commands in RDBMS.
DDL commands:
1. CREATE
2. ALTER
3. DROP
4. RENAME
5. TRUNCATE
1. CREATE Command
CREATE is a DDL command used to create databases, tables, triggers and other
database objects.
Syntax to create a new table:
CREATE TABLE table_name
(
column_Name1 data_type ( size of the column ) ,
column_Name2 data_type ( size of the column) ,
column_Name3 data_type ( size of the column) ,
...
column_NameN data_type ( size of the column )

);
Suppose, you want to create a Student table with five columns in the SQL
database. To do this, you have to write the following DDL command:

Example 1:
CREATE TABLE Student
(
Roll_No. int ,
First_Name varchar (20) ,
Last_Name varchar (20) ,
Age int ,
Marks int,
Dob Date
);
SQL>desc Student;
Example 2:
create table Employee
(
empid varchar(10),
empname varchar(20) ,
gender varchar(7),
age number(3),
dept varchar(15) ,
doj Date
);
SQL> desc Employee

Example 3:
create table BOOK
(
Book_id varchar(4),
Title varchar(10),
Publisher_name varchar(10),
Pub_year int
);
SQL> desc BOOK;
2. ALTER
This command is used to add, delete or change columns in the existing table. The
user needs to know the existing table name and can do add, delete or modify tasks
easily.

Syntax: –

ALTER TABLE table_name


ADD column_name datatype;

.ADD:
SQL> alter table employee add(designation varchar(15));
Table altered.
II.MODIFY
SQL> alter table employee modify (designation varchar(20));
Table altered
Example 1:
ALTER TABLE Student
ADD CGPA number;
SQL>desc Student;
Example 2:
ALTER TABLE Employee
ADD Salary number;

SQL>desc Employee;

Example 3:
ALTER TABLE BOOK
ADD Author_nmae varchar(20);

SQL>desc Student;
3. RENAME:
It is possible to change name of table with or without data in it using simple
RENAME command.
We can rename any table object at any point of time.

Syntax –
RENAME <Table Name> To <New_Table_Name>;

Example:
RENAME TABLE Employee To EMP;

4. TRUNCAT:
This command is used to remove all rows from the table, but the structure of the
table still exists.
Syntax –
Syntax to remove an existing table.

TRUNCATE TABLE table_name;

Example:
TRUNCATE TABLE Student;

5. DROP
This command is used to remove an existing table along with its structure from
the Database.
Syntax –
Syntax to drop an existing table.
DROP TABLE table_name;
Example: DROP TABLE Student_info;
DML(DATA MANIPULATION LANGUAGE):
Data manipulation language allows the users to query and manipulate data in
existing schema in object.
It allows following data to insert, delete, update and recovery data in schema
object.
DML COMMANDS:
❖ INSERT

❖ UPDATE

❖ DELETE
1. INSERT
This command is used to enter the information or values into a row. We can
connect one or more records to a single table within a repository using this
instruction.
Syntax:
Insert into Table_ Name Values(column1, column2, ....);

Example:
CREATE TABLE Student
(
Roll_No int ,
First_Name varchar (20) ,
Last_Name varchar (20) ,
Marks int,
Dob Date
);

SQL>desc Student;
SQL> insert into Student values(‘01’,’Adit,’k’’,25,’11-02-2004’);
SQL> insert into Student values(02,“Arpitha”,”S”, 20,’21-12-2003’);
SQL> insert into Student values(03,“Jorge”,”D”, 20, 18,’10-08-2001’);
Insert 2 more rows.
SQL>desc Student;
2. UPDATE
This allows the user to update the particular column value using the where
clause condition. This command is used to alter existing table records.
Syntax:
UPDATE <table_ name>
SET <column_ name = value>
WHERE condition;
Example:

UPDATE Students
SET Marks= 21
WHERE First_ name = “Arpitha”;

SQL>desc Students;

3. DELETE
a) Delete some rows
DELETE statement is used to delete rows from a table. Generally DELETE
statement removes one or more records from a table.
Syntax:
DELETE FROM table_name WHERE condition;
Example:
Delete from Students where Roll_no=’111’;

b) Delete All rows:


● It will remove all the rows from the table and does not free the space
contained by the table.
Syntax:
DELETE FROM table_name;
Example:
Delete from student;

DQL(Data Query Language)


DQL stands for the. DQL command is used for fetching the data. DQL command is
used for selecting data from the table, view, temp table, table variable, etc. There
is only one command under DQL which is the SELECT command.
Syntax :SELECT * FROM Employee;

The SELECT statement can be used in many ways.

1. Selecting some columns :


To select specified number of columns from the table the Following command is
used.
Syntax:
SELECT column_ name FROM table_ name;
Example:
Select First_ name, Last_ name from Students;

SQL> desc Students;


2. Select All Columns:
To select all columns from the table * is used instead of column names.
Syntax:

SELECT * FROM table_name;

Example:
Select * from Students;
SQL> desc Students;

3. Select using DISTINCT:


The DISTINCT keyword is used to return only different values (i.e. ) this
command does not select the duplicate values from the table.
Syntax:
SELECT DISTINCT column name(s) FROM table_name;

Example:
SELECT DISTINCT Roll_No FROM Students;

4. Select using IN:


If you want to get the rows which contain certain values, the best way to do it is
to use the IN conditional expression.
Syntax:
SELECT column name(s) FROM table_name
WHERE Column name IN (value1, value2,……,value-n);

Example:
SELECT * FROM students
WHERE students_name IN ( Arpitha, Jorge);

5. Select using BETWEEN:


BETWEEN can be used to get those items that fall within a range.
Syntax:
SELECT column name FROM table_name
WHERE Column name BETWEEN value1 AND value2;
Example:
SELECT * FROM student WHERE mark BETWEEN 80 and 100;

6. Renaming:
The select statement can be used to rename either a column or the entire table.
Syntax:
Renaming a column:
SELECT column name AS new name FROM table_name;

Example:

SELECT First_name As Name FROM Students;

Renaming a table:

RENAME old_table_name TO new_table_name;

Example:
RENAME Student TO Stu_details;

7. SELECT DATE
It is used to retrieve a date from a database. If you want to find a particular date
from a database, you can use this statement.
Syntax:
SELECT Column_Names(S) from table-name
WHERE condition(date_column);

Example: SELECT * FROM Students WHERE DOB >= '11-12-2000';

8. SELECT NULL
Null values are used to represent missing unknown data.
There can be two conditions:

1. Where SQL is NULL


2. Where SQL is NOT NULL

If in a table, a column is optional, it is very easy to insert data in column or update


an existing record without adding a value in this column. This means that field has
null value.
Where SQL is NULL:
Syntax:
SELECT COLUMN_NAME(S) FROM TABLE_NAME
WHERE COLUMN_NAME IS NULL;
Example:
SELECT Student_name, Marks FROM STUDENTS
WHERE MARKS IS NULL;

Where SQL is NOT NULL:

Syntax:
SELECT COLUMN_NAME(S) FROM TABLE_NAME
WHERE COLUMN_NAME IS NOT NULL;
Example:
SELECT Student_name, Marks FROM STUDENTS
WHERE MARKS IS NOT NULL;

ORDER BY Clause
ORDER BY is a clause in SQL which shows the result-set of the SELECT statement
in either ascending or descending order.
a) with one row
Syntax:
SELECT Column_Name FROM Table_Name
ORDER BY Column_Name;
Example:
SELECT * FROM Students ORDER BY Reg_no;

b) With Multiple row


Syntax:
SELECT Column_Name(S) FROM Table_Name
ORDER BY Column_Name(S);
Example:
SELECT reg_no,first_name,marks FROM Students ORDER BY first_name,
marks;
c) Ascending Order(ASC)/Descending
Order(DESC) SELECT Column_Name(S) FROM
Table_Name ORDER BY Column_Name(S) ASC;
Example:
SELECT * FROM Students ORDER BY Marks ASC;

d) with WHERE Clause


Syntax:
SELECT Column_Name(S)FROM Table_Name

WHERE [condition] ORDER BY Column_Name [ASC | DESC];


Example:
SELECT * FROM Students WHERE Marks >80 ORDER BY Marks;

GROUP BY clause
GROUP BY is an SQL keyword used in the SELECT query for arranging the same
values of a column in the group by using SQL functions.
Syntax:
SELECT Column_Name(S) FROM Table_Name
GROUP BY Column_Name(S);
Example:
SELECT COUNT (marks), Student_name GROUP BY marks;

a) with MIN clause


Group by with MIN clause shows the minimum value for the given where
clause.
Syntax:
SELECT MIN(Column_Name) FROM Table_Name;
Example:
SELECT MIN (Marks) FROM Students;
b) with MAX clause
Group by with MIN clause shows the minimum value for the given where
clause.
Syntax:
SELECT Column_Name, MAX(Column_Name) FROM Table_Name;
Example:
SELECT Stu_Subject, MAX (Marks) FROM Students;

c) COUNT() Function
The COUNT() function returns the number of rows in a database table.
Syntax:
COUNT(*)
or
COUNT( [ALL|DISTINCT] expression )

Example:
1. SELECT COUNT(*)
FROM Student;

2. SELECT COUNT(Marks)
FROM Student
GROUP BY marks
HAVING COUNT(*) > 50;

d) SUM() clause
It is used to return the total summed value of an expression.
Syntax:
SELECT SUM(aggregate_expression)
FROM tables
[WHERE conditions];
Example:
SELECT SUM(Marks)
FROM Student
Where roll_no=’111’;
DCL (DATA CONTROL LANGUAGE)
DCL stands for data control language. DCL commands are used
for providing and taking back the access rights on the database
and database objects. DCL command used for controlling user’s
access on the data. Most used DCL commands are GRANT and
REVOKE

GRANT
used to provide access right to the user.

Syntax: GRANT INSERT, DELETE ON Employee TO user;

REVOKE
REVOKE command is used to take back access right from the
user, it cancels access right of the user from the
database object.

Syntax
REVOKE ALL ON Employee FROM user;

TCL ( Transaction Control Language)


TCL commands are used for handling transactions in
the database. Transactions ensure data integrity in
the multi-user environment.

TCL commands can rollback and commit data modification in


the database. The most used TCL commands are COMMIT,
ROLLBACK, SAVEPOINT, and SET TRANSACTION.

COMMIT
COMMIT command is used to save or apply the modification in the
database.

ROLLBACK
ROLLBACK command is used to undo the modification.

SAVEPOINT
SAVEPOINT command is used to temporarily save a transaction,
the transaction can roll back to this point when it's needed.
Syntax :
Just write COMMIT or ROLLBACK or SAVEPOINT

You might also like