0% found this document useful (0 votes)
4 views

DML

The SQL Server lab manual provides instructions on Data Manipulation Language (DML) operations such as inserting, updating, and deleting values in tables, along with examples. It also covers Data Retrieval Language (DRL) using SELECT statements, comparison operators, and the LIKE operator for pattern matching. Additionally, it explains how to group and order results, use logical conditions in queries, create views, and calculate averages, maximums, and minimums.

Uploaded by

Chanyalew 21
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

DML

The SQL Server lab manual provides instructions on Data Manipulation Language (DML) operations such as inserting, updating, and deleting values in tables, along with examples. It also covers Data Retrieval Language (DRL) using SELECT statements, comparison operators, and the LIKE operator for pattern matching. Additionally, it explains how to group and order results, use logical conditions in queries, create views, and calculate averages, maximums, and minimums.

Uploaded by

Chanyalew 21
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL Server lab manual

DML (Data Manipulation Language)


Insert values
 Destination columns should match.
 if no value is given for a column, null will be taken by default, else if that column has “not null”
constraint, it returns error

The syntax to insert values within a table

Syntax: INSERT INTO TableName values(‘value1’,’value2’,’value3’) or


INSERT INTO TableName(column1, column2, column3) VALUES (‘value1’,’value2’,’value3’)
Example: insert into Coursetbl values('c005',’Database’, 3); Or
INSERT INTO Coursetbl(Ccode, CTitle, CCredit) VALUES(‘c005’,’Database’,’3’);

Update values in a table


To update a single field with in a table we can use the following syntax:

Syntax: Update TableName set ColumnName=’TheNewValue’ Where PrimaryKey=’TheValue Of


The Primary Key For That Column’;
Example: update Coursetbl set CTitle='computer organization and architecture' where Ccode='c001';
Delete a single row in a table

Example: delete from Coursetbl where Ccode='c001';


DRL (Data Retrieval Language)
Select statement
 The SELECT operator can be used, among other things, to display a value.
 Based on this, to use it, where it is needed, type SELECT followed by a number, a word, a string,
or an expression.

select * from Coursetbl;


select * from StudStatustbl;
select * from CourseRegistrationtbl;
select Studenttbl.Studname, Studenttbl.StudDepartment, Resulttbl.LetterGrade from
Studenttbl,Resulttbl where Resulttbl.Sid=Studenttbl.Sid;
SQL Server: Comparison Operators

Page 1
SQL Server lab manual

This SQL Server tutorial explores all of the comparison operators used to test for equality and
inequality, as well as the more advanced operators in SQL Server (Transact-SQL).
Comparison operators are used in the WHERE clause to determine which records to select. Here is a
list of the comparison operators that you can use in SQL Server.

Comparison Operator Description


= Equal
<> Not Equal
!= Not Equal
> Greater Than
>= Greater Than or Equal
< Less Than
<= Less Than or Equal

Equality Operator
In SQL Server, you can use the = operator to test for equality in a query.

Example: SELECT * FROM employees WHERE first_name = 'Jane';


In this example, the SELECT statement above would return all rows from the employees table where
the first_name is equal to Jane.
Inequality Operator
In SQL Server, you can use the <> or != operators to test for inequality in a query.
For example, we could test for inequality using the <> operator, as follows:

Example: SELECT * FROM employees WHERE first_name <> 'Jane';

In this example, the SELECT statement would return all rows from the employees table where
the first_name is not equal to Jane.
Or you could also write this query using the != operator, as follows:

Example: SELECT * FROM employees WHERE first_name != 'Jane';

Both of these queries would return the same results.


Greater Than Operator
You can use the > operator in SQL Server to test for an expression greater than.

Example: SELECT * FROM employees WHERE employee_id > 3000;

Page 2
SQL Server lab manual

In this example, the SELECT statement would return all rows from the employees table where
the employee_id is greater than 3000. An employee_id equal to 3000 would not be included in the
result.
Greater Than or Equal Operator
In SQL Server, you can use the >= operator to test for an expression greater than or equal to.

SELECT * FROM employees WHERE employee_id >= 3000;

In this example, the SELECT statement would return all rows from the employees table where
the employee_id is greater than or equal to 3000. In this case, employee_id equal to 3000 would be
included in the result set.
Less Than Operator
You can use the < operator in SQL Server to test for an expression less than.

SELECT * FROM employees WHERE employee_id < 500;

In this example, the SELECT statement would return all rows from the employees table where
the employee_id is less than 500. An employee_id equal to 500 would not be included in the result set.
Less Than or Equal Operator
In SQL Server, you can use the <= operator to test for an expression less than or equal to.

SELECT * FROM employees WHERE employee_id <= 500;

In this example, the SELECT statement would return all rows from the employees table where
the employee_id is less than or equal to 500. In this case, employee_id equal to 500 would be included
in the result set.
How to assign our own name for the columns in the database?

Example: select Studname as "Full Name", StudCollege " as College", StudGender as "Sex" from
Studenttbl;

SQL: LIKE
The LIKE operator of SQL is used to specify a pattern to select one or more records from a table or a
view.
If you want to match any character, in any combination, for any length, use the % wildcard.
If you precede it with a letter, use letter%.
If you set the letter in between (% letter %) with a letter.
Page 3
SQL Server lab manual

Example:
select StudName,StudAge from Studenttbl where Studname like 'm%';
// display student name from student table where student name starts with ‘m’

select StudName, StudAge from Studenttbl where Studname like '%m';


// display sstudent name from student table where student name end with ‘m’

select StudName, StudAge from Studenttbl where Studname like '%m%';


// display sstudent name from student table where student name has ‘m’ letter in between.

select StudName, StudAge from Studenttbl where Studname like '_t%';


// displays student name from student table where second letter name is t.
select StudName, StudAge from Studenttbl where Studname not like '_t%';

Group by and Order By

Example: Select studCollege, count(*) as NumOfStud From Studenttbl Group by StudCollege;


Example: Select * from Studenttbl order by studAge desc;

Using AND in where condition

Example: SELECT * FROM emp WHERE lname = 'Elias' AND empid >= 3000;

In this case, this SELECT statement uses the AND condition to return all employees that have
a last_name of 'Elias' and the employee_id is greater than or equal to 3000.
Using OR in where condition

Example: SELECT empid, lname, fname FROM emp WHERE lname = 'alemu' OR fname = 'elias';

In this case, this SELECT statement would return all employee_id, last_name, and first_name values
from the employees table where the last_name is 'alemu' or the first_name is 'Elias'.
Combining AND & OR conditions

Example: SELECT * FROM emp WHERE (city = ‘AA’ AND lname = ‘Samuel’) OR (empid = 82);

This example would return all employees that reside in the city of 'Debre Markos' and
whose last_name is 'Samuel' as well as all employees whose employee_id is equal to 82.

Page 4
SQL Server lab manual

How to create view

Example: CREATE VIEW statusreport AS SELECT Sid AS Expr1 FROM Studenttbl;

Average, Maximum and Minimum

Example: Select avg(studstatustbl.SGPA) as averageGrade, MAX(studstatustbl.SPGA) as SemsterTop


Max(studstatus.CGPA) as YearTOP From studstatustbl;

DISTINCT

Select COUNT(DISTINCT studenttbl.studname) as ‘NO OF Students’ From studenttbl Where


studGender like ‘[m or M] %’;

Page 5

You might also like