Database Administration and Management: SQL Lab
Database Administration and Management: SQL Lab
and Management
SQL LAB
1
Create Table
CREATE TABLE STUDENT1
( ROLL_NO NUMBER(6) PRIMARY KEY,
F_NAME CHAR(15) NOT NULL,
L_NAME CHAR(15) NOT NULL,
AGE CHAR(4),
DEP CHAR (15),
CITY CHAR (15),
PH_NO NUMBER(13) );
2
EXAMPLE
3
INSERT STATEMENT
The insert command add rows to a table. The format is:
VALUES
4
SELECT STATEMENT
• To view the table, you use SELECT command and it is most
commonly used in SQL. It allows database users to retrieve the
specific information they desire from an operational database.
5
EXAMPLE
6
WHERE Clause
• The WHERE clause is used with SELECT statement to retrieve only those
records from tables that fulfill or satisfy the given criteria.
• The format is:
7
Character Strings and Dates
Character strings and date values are enclosed in single quotation marks
Character values are case sensitive and date values are format sensitive
The default date format is DD-MON-YY
SQL>
SQL> SELECT
SELECT last_name,
last_name, job_id,
job_id, salary
salary
FROM
FROM employees
employees
WHERE
WHERE last_name
last_name == ‘King';
‘King';
8
Arithmetic Expressions
Create expressions on NUMBER data by using arithmetic
operators
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
9
Using Arithmetic Operators
SQL> SELECT last_name, salary, salary+300
FROM employees;
.............
10
Operator Precedence
__
**
// ++
11
Operator Precedence
SQL> SELECT first_name, salary, 12*salary+100
FROM employees;
.............
12
Using Parentheses
SQL> SELECT first_name, salary, 12*(salary+100)
FROM employees;
.............
13
Defining a Null Value
A null is a VALUE that is unavailable, unassigned, unknown, or
inapplicable
A null is not the same as zero or a blank space
.............
.............
14
Null Values in Arithmetic Expressions
Arithmetic expressions containing a null value evaluate to null
15
DISTINCT
The select keyword allow us to grab all information from a column on a
table. This means that there will be redundancies.
To eliminate duplicate rows, DISTINCT keyword is used.
The syntax is as follow:
16
Using Distinct keyword
SQL> SELECT DISTINCT department_id
FROM employees;
17
Defining a Column Alias
• Renames a column heading
• Is useful with calculations
• Immediately follows the column name - there can be also
be the optional AS keyword between column names and
alias
• Requires double quotation marks if it contains spaces or
special characters or is case sensitive.
18
Defining a Column Alias
SQL> SELECT last_name as name, commission_pct comm
FROM employees;
19
Concatenation Operator
• Concatenates columns or character strings to other
columns
• Is represented by two vertical bars (||)
• Creates a resultant column that is a character
expression
20
Using the Concatenation Operator
21
Literal Character Strings
A literal is a character, expression, or number included
in the SELECT list
22
Using Literal Character Strings
SQL> SELECT last_name||' '||'is a'||' '||job_id
AS "Employee Details"
FROM employees;
23
Comparison Operators
Operator Meaning
= Equal to
24
Using the Comparison
Operators
SQL> SELECT last_name, salary
FROM employees
WHERE salary <= 2500;
25
Using the AND, OR, NOT
Operators
Operator Function
AND It is used to combine two conditions and returns
true if both conditions are true.
OR It is used to combine two conditions and returns
true if either of the conditions is true.
List the name and salary of all the employees with salaries
greater than or equal to 4000 and less than or equal to
5000.
Example
34
Aggregate Functions
SQL> Select count(ename) from emp;
35
Aggregate Functions
36
Aggregate Functions
SQL> Select sum(sal) from emp;
37
Aggregate Functions
SQL> Select max(sal) from emp;
38