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

SQL_Examples

Uploaded by

sourav1971sarkar
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)
8 views

SQL_Examples

Uploaded by

sourav1971sarkar
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/ 5

SQL (Structured Query Language)

SQL (Structured Query Language) is the language used in RDBMS for


writing queries. Using SQL, a user can create queries to fetch and
manipulate the data of the database.
The SQL commands are of two types (according to syllabus):
1.Data Definition Language (DDL)
2.Data Manipulation Language (DML)
Data Definition Language (DDL)
These commands are used to define and modify the structure of a
database. The commands that fall under this category are listed as
follows:
1. CREATE– It is used to create a new database or table.
CREATE DATABASE
SYNTAX: CREATE DATABASE <DATABASE_NAME>;
EXAMPLE: CREATE DATABASE myschool_db;
USE DATABASE – To create a table USE Database command is required.
SYNTAX: USE <DATABASE_NAME>;
EXAMPLE: USE myschool_db;
CREATE TABLE
SYNTAX: CREATE TABLE table_name(column_definition1, column_definition2, ........
,........);
EXAMPLE: CREATE TABLE employee(e_id int(8), e_name char(30));
Que:Write a command and query to create the structure of a table named
“student1”.
Roll_n Clas Nam
Marks
o s e
Ans:CREATE TABLE Student1 (Roll_No INT, Class CHAR, Name CHAR, Marks INT);
2. ALTER– It is used to modify the structure of an existing database or
table.
TO ADD COLUMN (FIELD)
SYNTAX: ALTER TABLE <Table_Name> ADD COLUMN <Column_Name>
<Data_Type>;
EXAMPLE: ALTER TABLE employee ADD COLUMN e_loc char(50);
TO REMOVE/DROP COLUMN (FIELD)
SYNTAX: ALTER TABLE <Table_Name> DROP COLUMN <Column_Name>;
EXAMPLE: ALTER TABLE employee DROP COLUMN e_loc;
3. DROP – Deletes an existing database or table.
TO DROP TABLE
SYNTAX: DROP TABLE <Table_Name>;
EXAMPLE: DROP TABLE employee;
TO DROP DATABASE
SYNTAX: DROP DATABASE <Database_Name>;
EXAMPLE: DROP DATABASE myschool_db;
4. TRUNCATE – Remove all table records including allocated table spaces,
but not the table itself.
SYNTAX: TRUNCATE TABLE <Table_Name>;
EXAMPLE: TRUNCATE TABLE employee;
5. RENAME – It is used to change the name of existing database or table.
SYNTAX: ALTER TABLE <Table_Name> RENAME TO <New_Table_Name>;
EXAMPLE: ALTER TABLE employee RENAME TO customers;

Data Manipulation Language (DML)


A DML (Data Manipulation Language) is a computer programming
language used for adding (inserting), modifying (updating) and data in
database. The commands that fall under this category are listed as
follows:
1. INSERT – The insert command is used to add one or more records to a
table. There are two methods to use the INSERT command.
Method 1:
SYNTAX: INSERT INTO <Table_Name> (Field 1, Field 2,….) VALUES (Value 1, Value
2,….);
EXAMPLE: INSERT INTO employee (‘e_id’, ‘e_name’,) VALUES (1, “Mukesh”);

Method 2:
SYNTAX: INSERT INTO <Table_Name> VALUES (Value 1, Value 2,….);
EXAMPLE: INSERT INTO employee VALUES (1, “Mukesh”);
2. SELECT – It is used to retrieves or fetch the data from the table.
TO FETCH ENTIRE RECORD OF TABLE [* means ALL records]
SYNTAX: SELECT * FROM <Table_Name>;
EXAMPLE: SELECT * FROM employee;
TO FETCH RECORD OF SELECTED FIELDS FROM TABLE
SYNTAX: SELECT <Field1, Field2,…> FROM <Table_Name>;
EXAMPLE: SELECT e_name, e_id FROM <Table_Name>;

SELECT Command Clauses


•WHERE clause specifies a criteria about the rows to be retrieved.
•ORDER BY clause specifies an order (ascending/descending) in which the
rows (records) are to be retrieved.
To retrieve all records:
SELECT * FROM <Table_Name>;
To retrieved records from specific fields:
SELECT Field1, Field2,… FROM <Table_Name>;
By using WHERE clause:
SELECT * FROM <Table_Name> WHERE condition;
SELECT Field1, Field2,… FROM <Table_Name> WHERE condition;
By using ORDER BY clause:
By default Ascending order
SELECT * FROM <Table_Name> ORDER BY EName;
in Descending order
SELECT * FROM <Table_Name> ORDER BY EName DESC;
3. UPDATE – Sometimes, you need to modify the existing records in the
table. The UPDATE command of the SQL can be used for this purpose.
SYNTAX: UPDATE <Table_Name> SET Column1 = Value1, Column2 = Value2,….
WHERE condition;
EXAMPLE: UPDATE employee SET Last_Name=‘Sharma’ WHERE E_ID=101;
4. DELETE – It is used to remove the existing records from a table.
SYNTAX: DELETE FROM <Table_Name> WHERE condition;
EXAMPLE: DELETE FROM employee WHERE E_ID=101;
Important: CREATE TABLE BY ASSIGNING PRIMARY KEY
EXAMPLE: CREATE TABLE Employee1 (Emp_ID INT NOT NULL PRIMARY KEY, ENAME
VARCHAR(20), AGE INT(5));

MYSQL Functions
MySQL has many built-in functions. Some of them are as: COUNT( ),
SUM( ), AVG( ), MAX( ), MIN( )
COUNT( ) – The COUNT() function returns the number of rows that
matches a specified criterion.
SELECT COUNT(*) FROM table_name WHERE condition;
SELECT COUNT(column_name) FROM table_name WHERE condition;
SUM( ) – The SUM() function returns the total sum of a numeric column.
SELECT SUM(column_name) FROM table_name WHERE condition;
AVG( ) – The AVG() function returns the average value of a numeric
column.
SELECT AVG(column_name) FROM table_name WHERE condition;
MAX( ) – The MAX() function returns the largest value of the selected
column.
SELECT MAX(column_name) FROM table_name WHERE condition;
MIN( ) – The MIN() function returns the smallest value of the selected
column.
SELECT MIN(column_name) FROM table_name WHERE condition;
SQL Queries Examples

You might also like