Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
48 views

Database Administration and Management: SQL Lab

This document provides an overview of SQL commands for database administration and management. It covers topics such as creating tables, inserting data, selecting data, using where clauses, arithmetic expressions, null values, aggregate functions, and more. Examples are provided for each SQL concept discussed to demonstrate their proper usage.

Uploaded by

Saleha Khalid
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Database Administration and Management: SQL Lab

This document provides an overview of SQL commands for database administration and management. It covers topics such as creating tables, inserting data, selecting data, using where clauses, arithmetic expressions, null values, aggregate functions, and more. Examples are provided for each SQL concept discussed to demonstrate their proper usage.

Uploaded by

Saleha Khalid
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 38

Database Administration

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:

INSERT INTO STUDENT1

VALUES

(‘ 401 ‘, ’Ayesha’, ’Abid’, ’20’, ’CS’, ‘Lahore’, ‘03243564288’ );

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.

• The format is:


SELECT “column_name” FROM “table_name”;

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:

SQL> SELECT first_name, salary


FROM employees
WHERE department_id=‘60';

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
__

**
// ++

 Multiplication and division take priority over addition and


subtraction
 Operators of the same priority are evaluated from left to
right
 Parentheses are used to force prioritized evaluation and to
clarify statements

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

SQL> SELECT last_name, job_id, salary, commission_pct


FROM employees;

.............

.............

14
Null Values in Arithmetic Expressions
 Arithmetic expressions containing a null value evaluate to null

SQL> select last_name, 12*salary+commission_pct


from employees
WHERE last_name = ‘King';

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:

SELECT DISTINCT column_name


FROM “table_name”.

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;

SQL> SELECT last_name “Name”, 12*salary+commission_pct “Annual Salary”


FROM employees
WHERE last_name = 'King';

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

SQL> SELECT last_name||job_id AS "Employees"


FROM employees;

21
Literal Character Strings
 A literal is a character, expression, or number included
in the SELECT list

 Date and character literal values must be enclosed


within single quotation marks

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

> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

<> Not 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.

NOT It is used to reverse the result of the condition.


Using the AND, OR, NOT
Operators

SQL> Select * from emp


where sal > 1000 AND job = 'Clerk';
Using the AND, OR, NOT
Operators

SQL> Select * from emp


where sal > 1000 AND job = 'Clerk' OR job = 'Manager';
Using the AND, OR, NOT
Operators

SQL> Select * from emp


where NOT (job = 'Clerk' OR job = 'Manager');
Using the BETWEEN Operator
• In order to select data that is within a range of values, the
“BETWEEN” operator is used.
• The BETWEEN operator allows the selection of rows
that contain values within a specified lower and upper
limit.
• The range specified after the word between is inclusive.
• The lower value must be specified first.
• You can use the WHERE clause and the AND operator.
Example

List the name and salary of all the employees with salaries
greater than or equal to 4000 and less than or equal to
5000.

SQL> SELECT first_name, salary


2 FROM employees
3 WHERE salary BETWEEN 4000 AND 5000;
Using the IN Operator
1. The IN operator helps reduce the need to use
multiple OR conditions.
2. It is used to test for values in a specified list.

Example

• SQL> SELECT employee_id, first_name,


last_name
• FROM employees
• WHERE manager_id IN (120, 121, 122);
• In this query, you could obtain the same answer
by using the condition WHERE department_id =
120 OR department_id = 121 OR department_id
= 122.
Aggregate Functions
Functions Def
COUNT Used to count the number of rows of a given table. Also
used to count the total number of values of specific
column.
SUM Returns the sum of the values of specific column of
numeric data type. Null values are ignored.

AVGMAX Returns the average of the values of specific column of


numeric data type. Null values are ignored.

MAX Returns the largest value in the specific column of numeric


data type.
MIN Returns the lowest value in the specific column of numeric
data type.

34
Aggregate Functions
SQL> Select count(ename) from emp;

SQL> Select count(*) from emp;

35
Aggregate Functions

SQL> Select avg(sal) from emp;

36
Aggregate Functions
SQL> Select sum(sal) from emp;

SQL> Select sum(sal) from emp where


job = 'Clerk';

37
Aggregate Functions
SQL> Select max(sal) from emp;

SQL> Select min(sal) from emp where

38

You might also like