Set 1 — DEPARTMENT Table (Basic SQL: CREATE, INSERT, SELECT, UPDATE, DELETE)
- CREATE TABLE
- DESC
- INSERT
- WHERE
- LIKE
- BETWEEN
- UPDATE
- DELETE
- Subject
- MC01094031 RDBMS
- Tool
- Oracle 10g or above
- Questions
- 10
- Status
- Question list: official GTU PDF
Introduction
This is the first practical of the GTU MCA Semester 1 RDBMS lab. You create a single table, put a few department records in it and then ask the database simple questions. Every later practical builds on the commands you practise here.
Learning objectives
- Create a table using Oracle data types
NUMBERandVARCHAR2. - View a table's structure with the SQL*Plus
DESCcommand. - Insert, display, filter, update and delete rows.
- Use the
WHERE,LIKEandBETWEENoperators.
Original question list
- Create the Simple DEPARTMENT Table.
- Display structure of department table.
- Insert below records into Department Table: (10, Account, NY), (20, HR, NY), (30, Production, DL), (40, Sales, NY), (50, EDP, MU), (60, TRG, –), (110, RND, AH).
- Display all records of Department table.
- Display all department belonging to location 'NY'.
- Display details of Department 10.
- List all department names starting with 'A'.
- List all departments whose number is between 1 and 100.
- Delete 'TRG' department.
- Change department name 'EDP' to 'IT'.
Database schema
DEPARTMENT
| Column | Oracle type | Meaning / constraint |
|---|---|---|
dept_no | NUMBER(3) | Department number (primary key) |
dept_name | VARCHAR2(20) | Department name |
location | VARCHAR2(20) | City / location code |
Prerequisites
- Oracle 10g or above (SQL*Plus, SQL Developer or Oracle Live SQL).
- A user account with CREATE TABLE privilege.
- If a DEPARTMENT table already exists from an earlier attempt, drop it first:
DROP TABLE department;
Solutions
Q1 Create the Simple DEPARTMENT Table.
CREATE TABLE department (
dept_no NUMBER(3) PRIMARY KEY,
dept_name VARCHAR2(20) NOT NULL,
location VARCHAR2(20)
);Explanation
NUMBER(3) stores up to three digits (enough for 110). VARCHAR2(n) is Oracle's variable-length string type. The question asks for a *simple* table, so only a primary key and a NOT NULL constraint are added; constraints are studied in detail in Set 2.
Table created.
Q2 Display structure of department table.
DESC departmentExplanation
DESC (short for DESCRIBE) is a **SQL*Plus command**, not a SQL statement, so it does not need a semicolon. In SQL Developer you can run it the same way, or use the query on USER_TAB_COLUMNS shown in the note.
Name Null? Type ----------------------------------------- -------- ------------ DEPT_NO NOT NULL NUMBER(3) DEPT_NAME NOT NULL VARCHAR2(20) LOCATION VARCHAR2(20)
Q3 Insert below records into Department Table: (10, Account, NY), (20, HR, NY), (30, Production, DL), (40, Sales, NY), (50, EDP, MU), (60, TRG, –), (110, RND, AH).
INSERT INTO department VALUES (10, 'Account', 'NY');
INSERT INTO department VALUES (20, 'HR', 'NY');
INSERT INTO department VALUES (30, 'Production', 'DL');
INSERT INTO department VALUES (40, 'Sales', 'NY');
INSERT INTO department VALUES (50, 'EDP', 'MU');
INSERT INTO department VALUES (60, 'TRG', NULL);
INSERT INTO department VALUES (110, 'RND', 'AH');
COMMIT;Explanation
Each INSERT adds one row; the values must be listed in the same order as the columns were created. String values go inside single quotes. COMMIT makes the inserted rows permanent so that other sessions can see them.
1 row created. (printed 7 times) Commit complete.
Q4 Display all records of Department table.
SELECT * FROM department;Explanation
* means *all columns*. Without a WHERE clause, every row is returned.
| DEPT_NO | DEPT_NAME | LOCATION |
|---|---|---|
| 10 | Account | NY |
| 20 | HR | NY |
| 30 | Production | DL |
| 40 | Sales | NY |
| 50 | EDP | MU |
| 60 | TRG | |
| 110 | RND | AH |
7 rows selected.
Row order can differ in your Oracle session because this query has no ORDER BY.
Q5 Display all department belonging to location 'NY'.
SELECT * FROM department
WHERE location = 'NY';Explanation
String comparison in Oracle is case-sensitive: 'ny' would not match 'NY'. Use UPPER(location) = 'NY' if your data has mixed case. TRG, whose location is NULL, is never returned by a comparison — NULL is not equal to anything.
| DEPT_NO | DEPT_NAME | LOCATION |
|---|---|---|
| 10 | Account | NY |
| 20 | HR | NY |
| 40 | Sales | NY |
3 rows selected.
Row order can differ in your Oracle session because this query has no ORDER BY.
Q6 Display details of Department 10.
SELECT * FROM department
WHERE dept_no = 10;Explanation
Numbers are written without quotes. Because dept_no is the primary key, at most one row can be returned.
| DEPT_NO | DEPT_NAME | LOCATION |
|---|---|---|
| 10 | Account | NY |
1 row selected.
Q7 List all department names starting with 'A'.
SELECT dept_name FROM department
WHERE dept_name LIKE 'A%';Explanation
In LIKE, % matches any number of characters (including none) and _ matches exactly one character. 'A%' means *starts with A*.
| DEPT_NAME |
|---|
| Account |
1 row selected.
Q8 List all departments whose number is between 1 and 100.
SELECT * FROM department
WHERE dept_no BETWEEN 1 AND 100;Explanation
BETWEEN a AND b is inclusive of both ends, so it is the same as dept_no >= 1 AND dept_no <= 100. Department 110 (RND) is outside the range and is not shown.
| DEPT_NO | DEPT_NAME | LOCATION |
|---|---|---|
| 10 | Account | NY |
| 20 | HR | NY |
| 30 | Production | DL |
| 40 | Sales | NY |
| 50 | EDP | MU |
| 60 | TRG |
6 rows selected.
Row order can differ in your Oracle session because this query has no ORDER BY.
Q9 Delete 'TRG' department.
DELETE FROM department
WHERE dept_name = 'TRG';
COMMIT;Explanation
Always write the WHERE clause before running a DELETE — without it every row is deleted. Run the same condition with SELECT first to see which rows will be affected.
1 row deleted. Commit complete.
Verify the change
SELECT * FROM department ORDER BY dept_no;| DEPT_NO | DEPT_NAME | LOCATION |
|---|---|---|
| 10 | Account | NY |
| 20 | HR | NY |
| 30 | Production | DL |
| 40 | Sales | NY |
| 50 | EDP | MU |
| 110 | RND | AH |
6 rows selected.
Q10 Change department name 'EDP' to 'IT'.
UPDATE department
SET dept_name = 'IT'
WHERE dept_name = 'EDP';
COMMIT;Explanation
UPDATE ... SET changes column values in the rows that match WHERE. Only department 50 changes.
1 row updated. Commit complete.
Verify the change
SELECT * FROM department ORDER BY dept_no;| DEPT_NO | DEPT_NAME | LOCATION |
|---|---|---|
| 10 | Account | NY |
| 20 | HR | NY |
| 30 | Production | DL |
| 40 | Sales | NY |
| 50 | IT | MU |
| 110 | RND | AH |
6 rows selected.
Important concepts used
- DDL vs DML
CREATEis DDL (defines structure; auto-commits).INSERT,UPDATEandDELETEare DML (change data; needCOMMIT).- Case sensitivity
- Keywords and table names are not case-sensitive, but data inside quotes is.
- Inclusive BETWEEN
BETWEEN 1 AND 100includes both 1 and 100.- Wildcards
%= any sequence of characters,_= exactly one character.
Common mistakes
- Using double quotes for string values (
"NY"). Oracle treats double quotes as identifiers — use single quotes. - Writing
DELETE FROM department;without aWHEREclause and deleting every row. - Forgetting
COMMITand then wondering why another session (or a reconnect) cannot see the rows. - Putting a semicolon after
DESC departmentin some tools and getting an error —DESCis a SQL*Plus command. - Using
LIKE 'A*'—*is not a wildcard in SQL.
Viva questions
What is the difference between CHAR and VARCHAR2?
Is DESC a SQL command?
What is the difference between DELETE and TRUNCATE?
Why do we use COMMIT?
What does % mean in LIKE?
Is BETWEEN inclusive?
Summary
You created the DEPARTMENT table, viewed its structure, inserted the seven official departments (one with a NULL location) and practised SELECT with WHERE, LIKE and BETWEEN, followed by a controlled DELETE and UPDATE. Always check the affected rows with a SELECT before running DELETE or UPDATE, and COMMIT when the result is correct.
Download script
set-01-department-table.sql
Related practicals
Independent educational resource — not an official GTU publication. Verify questions and solutions with your current syllabus and lab instructor. Disclaimer