Lab Chapter 6
Lab Chapter 6
1
Prepared by: Worku Muluye
Here, column1, column2, ... are the field names of the table you want to select data from. If you
want to select all the fields available in the table, use the following syntax: SELECT * FROM
table_name;
6.2.1. SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a
table, a column often contains many duplicate values; and sometimes you only want to list the
different (distinct) values. The SELECT DISTINCT statement is used to return only distinct
(different) values.
SELECT DISTINCT Syntax: SELECT DISTINCT column1, column2, ... FROM table_name;
6.3. Update Statement
UPDATE statement is used to update data of the table within the database. It is used when you
need to modify the table. The UPDATE statement used to modify the data that is already in the
database. Which rows is to be update, it is decided by a WHERE clause.
Syntax: UPDATE table-name SET [column-name1=value1, …, column-nameN= valueN]
[WHERE condition];
Example: UPDATE student SET FirstName = 'Abebe' WHERE StudentID = '3'
6.3.1. 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:
Updating Single Column:
Example: UPDATE student SET StudentID = 001 WHERE FirstName = 'Abebe';
This SQL UPDATE example would update the StudentID to '001' in the student table where
FirstName is 'Abebe'.
Updating Multiple Columns:
To update more than one column with a single update statement:
Example: UPDATE students SET FirstName = 'Hana', City = 'Dessie' WHERE LastName = 'Tomas';
Examples: UPDATE student SET FirstName = 'Abebe', LastName = 'Yonas', City=’Bahir Dar’
WHERE StudentID = '3';
This SQL UPDATE statement will change the FirstName 'Hana' and City to 'Dessie' where the
LastName 'Tomas'.
2
Prepared by: Worku Muluye
6.3.2. Update Date
If you want to update a date & time field in SQL, you should use the following query. let's see
the syntax of SQL update date.
Syntax: UPDATE table SET Column-Name = 'YYYY-MM-DD HH:MM:SS' WHERE Id = value;
First, we take a table in which we want to update date and time fields. If you want to change the
first row which id is 1 then you should write the following syntax:
Example: UPDATE table SET EndDate = '2014-03-16 00:00:00.000' WHERE Id = 1
Note: you should always remember that SQL must attach default 00:00:00.000 automatically.
3
Prepared by: Worku Muluye
EMPLOYEE
EmpID Emp-Name Address Salary
001 Abebe Addis Ababa 15000
002 Dawit Adama 18000
Resulting table after SQL DELETE query:
6.4.2. Delete All Rows
The statement SQL DELETE ALL ROWS is used to delete all rows from the table. If you want
to delete all the rows from student table the query would be like,
DELETE FROM Employee;
Resulting table after using this query: Select * from Employee;
EmpID Emp-Name Address Salary
Review Questions
1. How to delete the data in the table?
2. What is the difference between delete and truncate statement?
3. How to delete a row from the table?
4. How to delete all the rows of a table?
5. How to delete the view from the database?
6. How to use delete statement with inner join?
7. How can we delete parent table records?
8. What is the difference between select, insert, update and delete statements?
9. Update tables by using join operator?
4
Prepared by: Worku Muluye