0% found this document useful (0 votes)
7 views31 pages

4.SQL Queries DML

The document provides an overview of SQL Data Manipulation Language (DML) commands, including INSERT, SELECT, UPDATE, and DELETE statements. It explains how to manipulate data in a database, detailing syntax, optional clauses, and the differences between DELETE, TRUNCATE, and DROP statements. Additionally, it covers filtering data with WHERE clauses and using SQL functions like COUNT and DISTINCT.

Uploaded by

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

4.SQL Queries DML

The document provides an overview of SQL Data Manipulation Language (DML) commands, including INSERT, SELECT, UPDATE, and DELETE statements. It explains how to manipulate data in a database, detailing syntax, optional clauses, and the differences between DELETE, TRUNCATE, and DROP statements. Additionally, it covers filtering data with WHERE clauses and using SQL functions like COUNT and DISTINCT.

Uploaded by

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

SQL QUERIES-2

DML-COMMANDS
• Data Manipulation Language (DML) statements are
used for managing data in database. DML
commands are not auto-committed. It means
changes made by DML command are not
permanent to database, it can be rolled back.
• SQL INSERT statement is a SQL query. It is used to
insert a single or a multiple records in a table.
• There are two ways to insert data in a table:
• By SQL insert into statement
– By specifying column names
– Without specifying column names.
• By SQL insert into select statement
1.SQL INSERT
Inserting data directly into a table

• If you specify the column names, syntax of the


insert into statement will be as follows:
• INSERT INTO TABLE_NAME (col1, col2, col3,.... col
N) VALUES (value1, value2, value 3, .... Value N);
• Here col1, col2, col3, .... colN are the columns of
the table in which you want to insert data.
Insert into statement without column
name
• INSERT into table-name values(data1,data2,..);
• Lets see an example,
• Consider a table Student with following fields.
• S_id,S_Name,age
• INSERT into Student values(101,'Adam',15);
• The above command will insert a record
into Student table.
Inserting data through SELECT
Statement
• Inserting data through SELECT Statement
• SQL INSERT INTO SELECT Syntax
INSERT INTO table_name [(column1, column2, .... column)] SELECT col
umn1, column2, .... Column N FROM table_name [WHERE condition];

• Note: when you add a new row, you should make


sure that data type of the value and the column
should be matched.
• If any integrity constraints are defined for the table,
you must follow them.
2.SQL SELECT
SQL SELECT
• The most commonly used SQL command is SELECT
statement. It is used to query the database and
retrieve selected data that follow the conditions we
want.
• In simple words, we can say that the select statement
used to query or retrieve data from a table in the
database.
• Let's see the syntax of select statement.
SELECT expressions FROM tables WHERE conditions;
Eg:select * from student where regno=1101
Optional clauses in SELECT statement
• There are some optional clauses in SELECT statement:
• [WHERE Clause] : It specifies which rows to retrieve.
• [GROUP BY Clause] : Groups rows that share a
property so that the aggregate function can be applied
to each group.
• [HAVING Clause] : It selects among the groups defined
by the GROUP BY clause.
• [ORDER BY Clause] : It specifies an order in which to
return the rows
FILTERING TABEL DATA
• To view the complete table syntax is:
• Select * from tablename;
• Eg: select * from student; (* is known as wildcard)
• Three ways of filtering table data are:
• A)Selected columns and all rows
• B)Selected rows and all columns
• C)Selected rows and selected columns.
1.SELECTED COLUMS AND ALL ROWS

• Syntax:
• SELECT <column1>,<column2> from <tablename>;
• Eg:
• select age,id from student;
2.Selected Rows and All Columns

• The select statement used untill now display all


rows.
• Oracle provides the option of using a WHERE Clause
in an sql query to apply filter on the rows reterived.
• Syntax:
• SELECT *from<tablename> WHERE<condition>;
• Eg:
• Select * from student where name=‘a’;
SQL WHERE
A WHERE clause in SQL is a data manipulation
language statement.
• WHERE clauses are not mandatory clauses of SQL
DML statements. But it can be used to limit the
number of rows affected by a SQL DML statement
or returned by a query.
• Actually. it filters the records. It returns only those
queries which fulfill the specific conditions.
• WHERE clause is used in SELECT, UPDATE, DELETE
statement etc.
• Let's see the syntax for sql where:
• SELECT column1, column 2, ... column n FROM table_name WHER
E [conditions]

• WHERE clause uses some conditional selection


= equal

> greater than

< less than

>= greater than or equal

<= less than or equal

<> not equal to


SQL AND
• The SQL AND condition is used in SQL query to create
two or more conditions to be met.
• It is used in SQL SELECT, INSERT, UPDATE and DELETE
statements.Let's see the syntax for SQL AND:
SELECT columns FROM tables WHERE condition 1 AND condition 2;

• The SQL AND condition requires that both conditions


should be met.
• The SQL AND condition also can be used to join
multiple tables in a SQL statement.
• Rest of AND CLAUSE will be discussed later on
3.SELECTED COLUMNS AND SELECTED COLUMNS
• To view specific set of rows and columns the syntax
is:
• SELECT<column1><column2> from <table
name>where<condition>;
• Eg;
• Select id,name from student where age=20;
SELECT DISTINCT
• The SQL DISTINCT command is used with SELECT
key word to retrieve only distinct or unique data.
• In a table, there may be a chance to exist a
duplicate value and sometimes we want to retrieve
only unique values. In such scenarios, SQL SELECT
DISTINCT statement is used.
• Note: SQL SELECT UNIQUE and SQL SELECT
DISTINCT statements are same.
• Let's see the syntax of select distinct statement.
• SELECT DISTINCT column_name FROM table_name;
• Let's try to understand it by the table given below:

Student_Name Gender Mobile_Numbe HOME_TOWN


r
Rahul Ojha Male 7503896532 Lucknow
Disha Rai Female 9270568893 Varanasi
Sonoo Jaiswal Male 9990449935 Lucknow

• Here is a table of students from where we want to retrieve distinct information For
example: distinct home-town.
• SELECT DISTINCT home_town FROM students
• Now, it will return two rows.

HOME_TOWN
Lucknow
Varanasi
SQL SELECT COUNT
• The SQL COUNT() function is used to return the
number of rows in a query.
• The COUNT() function is used with SQL SELECT
statement and it is very useful to count the number
of rows in a table having enormous data.
• For example: If you have a record of the voters in
selected area and want to count the number of
voters then it is very difficult to do it manually but
you can do it easily by using the SQL SELECT COUNT
query.
• Let's see the syntax of SQL COUNT statement.
• SELECT COUNT (expression) FROM tables WHERE conditions;
• SELECT COUNT(name) FROM employee_table;
• It will return the total number of names of employee_table. But null fields
will not be counted.
• SELECT COUNT(*) FROM employee_table;
• The "select count(*) from table" is used to return the number of records in
table.
• SELECT COUNT(DISTINCT name) FROM employee_table;
• It will return the total distinct names of employee_table.
SQL SELECT IN
• SQL IN is an operator used in a SQL query to help
reduce the need to use multiple SQL "OR"
conditions.
• It is used in SELECT, INSERT, UPDATE or DELETE
statement.
• Expression IN (value 1, value 2 ... value n);
• SELECT * FROM students WHERE regno IN ( 1101,
1103)
SQL Aliases-
• SQL aliases are used to temporarily rename a table
or a column heading.Basically aliases are created to
make column names more readable.
• To give alias name to column
• SELECT column_name AS alias_nameFROM table_name;
• Eg:select regno as id from student;
• SQL Alias Syntax for Tables(to be done with joins)
• SELECT column_name(s)FROM table_name AS alias_name;
• Eg:select
3.SQL UPDATE
3.SQL UPDATE
• SQL UPDATE statement is used to change the data
of the records held by tables. Which rows is to be
update, it is decided by a condition. To specify
condition, we use WHERE clause.
• The UPDATE statement can be written in following
form:
• UPDATE table_name SET [column_name1= value1,... column
_nameN = valueN] [WHERE condition]
• UPDATE students SET name = 'b' WHERE regno = ‘1101'
• Updating Multiple Fields:
• If you are going to update multiple fields, you
should separate each field assignment with a
comma.
• SQL UPDATE statement for multiple fields:
• UPDATE table_name SET field1 = new-
value1, field2 = new-value2, [WHERE CLAUSE]
• Update is used with JOIN also which we will do later
on
4.SQL DELETE
4. SQL DELETE
• The SQL DELETE statement is used to delete rows
from a table. Generally DELETE statement removes
one or more records from a table.
• SQL DELETE Syntax
• DELETE FROM table_name [WHERE condition];
• Here table_name is the table which has to be
deleted. The WHERE clause in SQL DELETE
statement is optional here.
• Eg: delete from table where regno=1101;
• But if you do not specify the WHERE condition it
will remove all the rows from the table.
• DELETE FROM table_name;
• Eg:delete from student;
• There are some more terms similar to DELETE
statement like as DROP statement and TRUNCATE
statement but they are not exactly same there are
some differences between them
Difference between DELETE and
TRUNCATE statements
• There is a slight difference b/w delete and truncate
statement. The DELETE statement only deletes the
rows from the table based on the condition defined
by WHERE clause or delete all the rows from the table
when condition is not specified.
• But it does not free the space containing by the table.
• The TRUNCATE statement: it is used to delete all the
rows from the table and free the containing space.
Difference between DROP and TRUNCATE

• When you use the drop statement it deletes the table's


row together with the table's definition so all the
relationships of that table with other tables will no
longer be valid.
• When you drop a table:
• Table structure,itsRelationship ,Integrity constraints
and Access privileges will also be dropped
• On the other hand when we TRUNCATE a table, the
table structure remains the same, so you will not face
any of the above problems.

You might also like