0% found this document useful (0 votes)
173 views47 pages

4.SQL Queries DML

The document discusses various SQL data manipulation language (DML) commands. It describes how the INSERT statement is used to insert records into tables, either by specifying column names or using SELECT statements. It also explains how the SELECT statement is used to query and retrieve data from tables using optional clauses like WHERE, ORDER BY, GROUP BY and HAVING. Finally, it covers the UPDATE and DELETE statements and how to use them to modify or remove existing records in database tables.

Uploaded by

Ankit Rajput
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)
173 views47 pages

4.SQL Queries DML

The document discusses various SQL data manipulation language (DML) commands. It describes how the INSERT statement is used to insert records into tables, either by specifying column names or using SELECT statements. It also explains how the SELECT statement is used to query and retrieve data from tables using optional clauses like WHERE, ORDER BY, GROUP BY and HAVING. Finally, it covers the UPDATE and DELETE statements and how to use them to modify or remove existing records in database tables.

Uploaded by

Ankit Rajput
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/ 47

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

Create another table:


C (ID, Name, Address, City, PinCode, Country)
Inserting data through SELECT
Statement
Syntax
INSERT INTO table_name  [(column1, column2, .... column)]  
SELECT column1, 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.
Inserting data through SELECT
Statement

insert into C select CustomerID, CustomerName,


Address, City, PostalCode, Country from Customers
where CustomerID >=1
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.
• [ORDER BY Clause] : It specifies an order in which to return
the rows
• [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.
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>;
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.
3.SELECTED COLUMNS
• To view specific set of rows and columns the syntax
is:
• SELECT<column1><column2> from <table
name>where<condition>;
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
Student_Name Gender Mobile_Numbe HOME_TOWN
r
Rahul Ojha Male 7503896532 Lucknow
Disha Rai Female 9270568893 Varanasi
Sonoo Jaiswal Male 9990449935 Lucknow

CREATE TABLE Persons (
    SName varchar(255),
    Gender varchar(255),
    Address varchar(255),
    Mobile_no int;
);

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 used to count the number of rows in a table having
enormous data.
• For example: If you have a record of the voters in a 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.
Syntax:
•SELECT COUNT (expression)  FROM tables  WHERE condit
ions;  

Example:
return the total number of names of customers.
SELECT COUNT(name) FROM Customers;  
Now add one more row with Name kept blank:

Null fields will not be counted.


Syntax:
•SELECT COUNT (expression)  FROM tables  WHERE conditions;  
Example:
SELECT COUNT(name) FROM Customers;  
•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.
First add same name entries
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,... colu
mn_nameN = valueN] [WHERE condition]  

UPDATE customers  SET name = 'b'  WHERE customerid = 101 ;
 
• 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]  
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.
delete from table where customerid=101;
• But if you do not specify the WHERE condition it
will remove all the rows from the table.
• DELETE FROM table_name;  
delete from student;
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, its Relationship ,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.
SQL ROWNUM Clause

SELECT TOP number|percent column_name(s)


FROM table_name WHERE condition;

SELECT * FROM CUSTOMERS WHERE ROWNUM <= 3;

Length()
SQL MIN() and MAX() Functions

The MIN() function returns the smallest value of the selected


column.
The MAX() function returns the largest value of the selected
column.
SQL MIN() and MAX() Functions

MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;

MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;
SQL MIN() and MAX() Functions

SELECT MIN(CUSTOMERID) AS SmallestID FROM CUSTOMERS;

SELECT MAX(CUSTOMERID) AS LargestID


FROM CUSTOMERS;
SQL AVG() and SUM() Functions

AVG() Syntax
SELECT AVG(column_name) FROM table_name WHERE
condition;
The SUM() function returns the total sum of a numeric column.

SUM() Syntax
SELECT SUM(column_name) FROM table_name WHERE
condition;
SQL LIKE Operator

The LIKE operator is used in a WHERE clause to search


for a specified pattern in a column.
There are two wildcards often used in conjunction with
the LIKE operator:

The percent sign (%) represents zero, one, or multiple


characters
The underscore sign (_) represents one, single character
SQL LIKE Operator

SELECT column1, column2, ...


FROM table_name
WHERE columnN LIKE pattern;

SELECT * FROM Customers


WHERE CustomerName LIKE 'a%';
SQL LIKE Operator
SQL LIKE Operator

1. select all customers with a CustomerName starting with "a"

SELECT * FROM Customers WHERE CustomerName LIKE 'a%';

2. select all customers with a CustomerName ending with "a"

SELECT * FROM Customers WHERE CustomerName LIKE '%a';

3. select all customers with a CustomerName that have "or" in any


position

SELECT * FROM Customers WHERE CustomerName LIKE '%or


%';
SQL LIKE Operator

4. select all customers with a CustomerName that have "r" in the


second position

SELECT * FROM Customers WHERE CustomerName LIKE '_r%';

5. select all customers with a CustomerName that starts with "a"


and are at least 3 characters in length:

SELECT * FROM Customers WHERE CustomerName LIKE 'a__


%';
SQL LIKE Operator

6. select all customers with a ContactName that starts with "a" and
ends with "o":

SELECT * FROM Customers WHERE ContactName LIKE 'a%o';

7. select all customers with a CustomerName that does NOT start


with "a":

SELECT * FROM Customers WHERE CustomerName NOT LIKE


'a%';
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;
SQL ROWNUM Clause

SELECT TOP number|percent column_name(s)


FROM table_name WHERE condition;

SELECT * FROM CUSTOMERS WHERE ROWNUM <= 3;

Length()

You might also like