4.SQL Queries DML
4.SQL Queries DML
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
• Syntax:
• SELECT <column1>,<column2> from <tablename>;
• Eg:
• select age,id from student;
2.Selected Rows and All Columns
• 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