You are on page 1of 3

SQL

DDL Statements(create table)


DML statements(Insert into, update, select,delete)
Insert: The SQL INSERT INTO Statement is used to add new rows of data to
a table in the database.
Examples
• Insert into “Details”(“Rno”,“NAme”, “Address”, “Gender”, “Availabiltiy of Transport”)
values(‘7’, ‘Tiya’, ‘12’, ‘F’, ‘True’);
• Create a table “Marks” with the following fields: Roll no, name and total marks.
create table "Marks"("Rno" int, "Name" varchar(30) ,"T_marks" int);

Delete: The SQL DELETE Query is used to delete the existing records from a
table.
You can use the WHERE clause with a DELETE query to delete the selected
rows, otherwise all the records would be deleted.
Examples
Write a query to delete the record of the student with roll no 10.
Delete from “details” where “Rno”=10;
Write a query to delete the records of all male students.
Delete from "details" where "gender" = 'm';

Select: Select is the most commonly used statement in SQL. The SELECT Statement in
SQL is used to retrieve or fetch data from a database. We can fetch either the entire
table or according to some specified rules.
Select with where Clause: The SQL WHERE clause is used to filter the results and
apply conditions
Example
Display the name and age of those students who are staying in Indirapuram.
Select Name, Age, Address from Details where Address='Indirapuram';
Select with order by Clause: The SQL ORDER BY clause is used to sort the records in
the result set for a SELECT statement.
ASC
ASC sorts the result set in ascending order
DESC
DESC sorts the result set in descending order
Example
1. Write a query to display roll no , age and name from table ‘Details’ and arrange the
records in descending order of Age.
select Rno, Name, Age from Details order by Age desc ;
Update Query:
Update statement is used for modifying records in a database. The general syntax of
the update statement is as follows:
UPDATE <table_name>
SET <column_name> = value [, column_name = value ...]
[WHERE <condition>];
Examples
Write a query to change the age of Tiya from 19 to 18.
Update “Details” set “Age”=18 where “Name”= ‘Tiya’;
Write a query to change the address from Vaishali to Ghaziabad where age is greater than 15.
Update “Details” set “Address”= ‘Ghaziabad’ where “Age”>15;
Write a query to increase age by one in the table Details for all the students.
Update “Details” set “Age”= “Age”+1;
Create Query
The CREATE TABLE statement is used to create a new table in a database.
CREATE TABLE table_name (
column1 datatype, column2 datatype, column3 datatype, ....);
Example:
Write an SQL query to create a table – Marks with the fields: Rno, Name and Total marks. Choose
the suitable data types also.
Create table “Marks”(“Rno” int,”Name” varchar(30), “Tmarks” int);

You might also like