DML
DML
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.
Equality Operator
In SQL Server, you can use the = operator to test for equality in a query.
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:
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.
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.
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.
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’
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
DISTINCT
Page 5