SQL Manual
SQL Manual
Institute of Technology
If you want the name of the database to be in different words, include them in square
Renaming a database
To change the name of a database, Transact-SQL provides sp_renamedb.
The ExistingName factor is the name of the database that you want to rename.
The NewName factor is the name you want the database to have after renaming it. Here
use RegistrarDB
create table Studenttbl
(Sid varchar(20) primary key, Studname varchar(50) not null, StudCollege
varchar(20) not null, StudAge int not null, StudGender varchar(6) not null);
OR
To create a UNIQUE constraint on the "PId" column when the table is already created,
use the following SQL:
The DEFAULT constraint can also be used to insert system values, by using functions like
GETDATE():
ALTER TABLE Persons ALTER COLUMN City SET DEFAULT 'Debre Markos'
To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple
columns, use the following SQL syntax:
To create a CHECK constraint on the "PId" column when the table is already created, use the
following SQL:
Renaming a table
To rename a table using code, execute the sp_rename stored procedure using the following
formula:
In this case, the interpreter would look for a table named StaffMembers in the current or
selected database. If it finds it, it would rename it Employees. If the table doesn't exist, you
Example:
use RegistrarDB
sp_help Resulttbl;
following formula:
On the right side of the ALTER TABLE expression, type the name of the table. On the right
side of the DROP COLUMN expression, enter the name of the undesired column. Here is an
example:
use RegistrarDB
alter table CourseRegistrationtbl drop RegID;
To Add a column
To add a new column to a table, follow this formula:
use RegistrarDB
alter table Coursetbl alter column CTitle varchar(100);
To drop a table
To delete a table using SQL, use the following formula:
Example:
use RegistrarDB
drop table employeetbl; or drop table registrarDB.dbo.employeetbl;
To truncate table
Description: The keyword truncate removes all values from the table once and cannot be
Example:
use RegistrarDB
truncate table StudStatustbl;
Insert values
Destination columns should match.
Example:
use RegistrarDB
use RegistrarDB
INSERT INTO [Coursetbl]([Ccode],[CTitle],[CCredit])
VALUES(‘c005’,’internet programming’,’3’);
Example:
use RegistrarDB
delete from Coursetbl where Ccode='c001';
Select statement
The SELECT operator can be used, among other things, to display a value. The SELECT
SELECT What;
string, or an expression.
To display a sentence using SELECT, type it in single-quotes on the right side of this
Use RegistrarDB
select * from Statussampleview;
select * from Resulttbl;
select * from CourseRegistrationtbl;
select * from Student_Backup;
select * from Coursetbl;
select * from StudStatustbl;
select Studenttbl.Studname,Studenttbl.StudDepartment, Resulttbl.LetterGrade from
Studenttbl,Resulttbl
where Resulttbl.Sid=Studenttbl.Sid;
Description
Here is a list of the comparison operators that you can use in SQL Server (Transact-SQL):
For example:
SELECT *
FROM employees
WHERE first_name = 'Jane';
In this example, the SELECT statement above would return all rows from the employees table
For example, we could test for inequality using the <> operator, as follows:
SELECT *
FROM employees
WHERE first_name <> 'Jane';
In this example, the SELECT statement would return all rows from the employees table where
Or you could also write this query using the != operator, as follows:
SELECT *
FROM employees
WHERE first_name != 'Jane';
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 3000. An employee_id equal to 3000 would not be included
SELECT *
FROM employees
WHERE employee_id >= 3000;
the employee_id is greater than or equal to 3000. In this case, n employee_id equal to 3000
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.
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, n employee_id equal to 500 would
use RegistrarDB
SELECT * from Studenttbl where [SID] = ALL (SELECT MAX (SID)
from Studenttbl)
How to assign our own name for the columns in the database?
Example:
use RegistrarDB
select Studname as "Full Name",StudCollege " as College",StudGender as "Sex"
Transact-SQL: LIKE
The LIKE operator of Transact-SQL is used to with a wildcard (missing letters in the pattern)
to specify a pattern to select one or more records from a table or a view. If you are visually
creating the statement, in the Criteria section, in the box corresponding to Filter for the
column on which the condition would be applied, type the LIKE condition. In Transact-SQL,
expression.
The pattern factor can be a value to be found in Expression. For example, it can be the same
type of value used in a WHERE statement. In this case, the equality operator would be the
same as LIKE. If you want to match any character, in any combination, for any length, use the
% wildcard. If you precede it with a letter, as in S%, the condition would consist of finding any
If you set the letter in between (% letter %) with a letter, as in %S%, the condition would consist
Example:
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 '[G or g]%'; //
displays student name from student table where name strats with upper case or
small case g.
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%';
use RegistrarDB
select studenttbl.sid from Studenttbl
where Studenttbl. StudCollege like '[T or t]%'
Intersect
(select StudStatustbl.Sid from StudStatustbl)
Example:
use RegistrarDB
select studenttbl.sid from Studenttbl
where Studenttbl.StudCollege like '[T or t]%'
union (select StudStatustbl.Sid from StudStatustbl)
Example:
Description: The SQL Server (Transact-SQL) WHERE clause is used to filter the results from a
Syntax: The syntax for the WHERE clause in SQL Server (Transact-SQL) is:
WHERE conditions;
examples. We'll start by looking at how to use the WHERE clause with only a single condition.
For example:
SELECT *
FROM employees
WHERE first_name = 'abebe';
In this SQL Server WHERE clause example, we've used the WHERE clause to filter our results
from the employees table. The SELECT statement above would return all rows from
fields from the employees table would appear in the result set.
For example:
SELECT *
FROM employees
WHERE last_name = 'Elias'
AND employee_id >= 3000;
This SQL Server WHERE clause example uses the WHERE clause to define multiple conditions.
In this case, this SELECT statement uses the AND condition to return all employees that have
For example:
This SQL Server WHERE clause example uses the WHERE clause to define multiple conditions,
but instead of using the AND condition, it uses the OR condition. 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'.
SELECT *
FROM employees
WHERE (city = 'Debre Markos' AND last_name = 'Samuel')
OR (employee_id = 82);
This SQL Server WHERE clause example uses the WHERE clause to define multiple conditions,
but it combines the AND condition and the OR condition. This example would return
all employees that reside in the city of 'Debre Markos' and whose last_name is 'Samuel' as well
The parentheses determine the order that the AND and OR conditions are evaluated. Just like
Example:
use RegistrarDB
select StudCollege, count(*) from Studenttbl group by StudCollege;
use RegistrarDB
select Studenttbl.Studname, Studenttbl.StudCollege,
StudStatustbl.CGPA, StudStatustbl.StudStatus from Studenttbl,StudStatustbl where
Studenttbl.Sid=StudStatustbl.Sid;
Description: The SQL Server (Transact-SQL) ORDER BY clause is used to sort the records in
your result set. The ORDER BY clause can only be used in SELECT statements.
Syntax: The syntax for the ORDER BY clause in SQL Server (Transact-SQL) is:
SELECT expressions
FROM tables
[WHERE conditions]
ORDER BY expression [ ASC | DESC ];
Note: The UNION operator selects only distinct values by default. To allow duplicate values,
use the ALL keyword with UNION.
SQL UNION ALL Syntax
Example:
SELECT CID FROM Customer UNION SELECT City FROM Supplier ORDER BY City;
SELECT City FROM Customer UNION ALL SELECT City FROM Supplier ORDER BY City;
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;
SELECT column_name(s)
FROM table1
or:
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
Example:
The right join returns all the rows from the second table (student Status table). Even if there
Example:
use RegistrarDB
select Studenttbl.Studname,StudStatustbl.StudStatus
from Studenttbl left join StudStatustbl ON
Studenttbl.Sid=StudStatustbl.Sid
Example:
use RegistrarDB
select Studenttbl.Studname,StudStatustbl.StudStatus
from StudStatustbl right join Studenttbl ON
Studenttbl.Sid=StudStatustbl.Sid
DISTINCT
Example
Select into
use RegistrarDB
SELECT Studenttbl.Studname,
Studenttbl.StudCollege,
StudStatustbl.CGPA INTO Student_Backup
FROM StudStatustbl , Studenttbl where Studenttbl.Sid=StudStatustbl.Sid
and Studenttbl.StudCollege like '[T or t ]%';
Top
Example:
getdate()
Example:
use RegistrarDB
select getdate() as Today;
use RegistrarDB
backup database RegistrarDB
TO DISK ='C:\Users\Lijalem\Desktop\Database\RegistrarDB.ldf';
examples.
Create VIEW
Syntax: The syntax for the CREATE VIEW statement in SQL Server (Transact-SQL) is:
For example:
This SQL Server CREATE VIEW example would create a virtual table based on the result set of
Syntax: The syntax for the ALTER VIEW statement in SQL Server (Transact-SQL) is:
Example
Here is an example of how you would use the ALTER VIEW Statement in SQL Server
(Transact-SQL):
dropping it in SQL Server. The VIEW must exist for you to be able to execute an ALTER VIEW
command.
Drop VIEW
Once a VIEW has been created in SQL Server, you can drop it with the DROP VIEW Statement.
Syntax: The syntax for the DROP VIEW statement in SQL Server (Transact-SQL) is:
DROP VIEW view_name;
Example: Here is an example of how to use the DROP VIEW Statement in SQL Server
(Transact-SQL):
(Transact-SQL).
transaction is rolled back to a save point, it must proceed to completion with more Transact-
altogether by rolling the transaction back to its beginning. To cancel an entire transaction,
use the form ROLLBACK TRANSACTION transaction_name. All the statements or procedures
of the transaction are undone. Duplicate save point names are allowed in a transaction, but a
ROLLBACK TRANSACTION statement that specifies the save point name will only roll the
transaction back to the most recent SAVE TRANSACTION using that name.
Example:
use RegistrarDB
begin transaction
save transaction s1
delete from Coursetbl
where Ccode='c002';
Rollback / Restoring
ROLLBACK TRANSACTION without a savepoint_name or transaction_name rolls back to the
beginning of the transaction. When nesting transactions, this same statement rolls back all
except when the COMMIT TRANSACTION is associated with a nested transaction that is
contained within the transaction being rolled back. In this instance, the nested transaction
transaction, duplicate save point names are allowed, but a ROLLBACK TRANSACTION using
the duplicate save point name rolls back only to the most recent SAVE TRANSACTION using
Example:
use RegistrarDB
begin transaction s1
rollback transaction
To Be Continued……