0% found this document useful (0 votes)
2 views

SQL-Basic queries

The document provides an introduction to SQL, covering its definition, data types, and various SQL queries including DML and DDL operations. It explains the structure of SQL queries, the use of operators in the WHERE clause, and the functionality of predefined functions. Additionally, it details how to perform INSERT, UPDATE, DELETE operations, and the concepts of COMMIT and ROLLBACK in SQL transactions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL-Basic queries

The document provides an introduction to SQL, covering its definition, data types, and various SQL queries including DML and DDL operations. It explains the structure of SQL queries, the use of operators in the WHERE clause, and the functionality of predefined functions. Additionally, it details how to perform INSERT, UPDATE, DELETE operations, and the concepts of COMMIT and ROLLBACK in SQL transactions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

SQL Queries-Session-1

CONTENTS
What is SQL?
DML and DDL
Select Query
Data types in SQL
Operators allowed in the where clause of SQL
Alias names for tables and columns
Predefined functions in SQL
INSERT query
UPDATE query
DELETE query
COMMIT and ROLLBACK
APPENDIX A,B,C
What is SQL?

SQL stands for Structured Query Language

SQL lets you access and manipulate databases

SQL is an ANSI standard(American National Standards Institute)

In oracle,the data is stored in RDBMS(Relational Database Management
System)

The data is stored in RDBMS in database objects called tables and it
consists of columns(fields) and rows(records).
EMPLOYEES TABLE

Emp_ID First_nam Last_nam Address City Experie


e e nce

332197 Nithya Kandappa pallikaranai Chennai 2


n

333118 Gayathri P Baby Nagar Chennai 2

471487 Thejas Patel Nandambakka Chennai 1


m

472256 Ramya Reddy velachery Chennai 1


DML and DDL
SQL queries can be classifies into two parts:
1. Data Definition Language (DDL):

CREATE TABLE - creates a new table

ALTER TABLE - modifies a table

DROP TABLE - deletes a table

CREATE INDEX - creates an index (search key)

DROP INDEX - deletes an index
2. Data Manipulation Language (DML) :

SELECT - extracts data from a database

UPDATE - updates data in a database

DELETE - deletes data from a database

INSERT INTO - inserts new data into a database
SELECT query
SYNTAX: SELECT columns FROM tables;
EXAMPLE:
select First_name,Last_name from employees;
select * from employees;
SQL is not case-sensitive
select * from employees;
is same as
SELECT * FROM employees;
DISTINCT keyword:
SYNTAX: SELECT DISTINCT columns FROM tables;
Example:
Select distinct city from employees ;
Data Types

Desc employees;
DATA TYPES:
1. DATE
2. NUMBER(size,precision)
3. VARCHAR(SIZE)
DATE TYPES:
Day: dd (01-31),dy(mon-sun),day(Monday-Sunday)
Month: mo (01-12),mon(Jan-Dec), Month(January-December)
Year: YY(10,11 etc),YYYY(2011,1989 etc)
Note:
Character is case-sensitive and Date is format sensitive. Default date
format 'dd-mon-yy'
SELECT query
Example:
Select salary,salary+200 “increment” from departments;
Select salary*12 annual_Salary from departments;
Select (salary+200)*12 “incremented annual salary” from departments;
Concatenation Operator:
Select First_name || Last_name as “Full_Name” from employees;
Select First_name || ' ' || Last_name as “Full_Name” from employees;
Select First_name || 'is in' || City from employees;
Alternative Quote Operator:
Select First_name || q'[it's first name]' || Last_name from employees;
Delimitors can be [] , {} , <>
Syntax : q'delimitor <string> delimitor '
Operator Description

= Equal to

<> or != Not equal to

> Greater than

< Less than

>= Greater than or


equal
Operators Allowed in the WHERE Clause
EXAMPLE:

select First_name,Last_name from employees where Emp_ID=332197;

select First_name,Last_name from employees where City='Chennai';

select * from employees where First_name='Thejas' and
Last_name='Patel';

select * from employees where First_name='Thejas' or
Last_name='Kumar';

select * from employees where First_name='Thejas' and
location='Chennai' or location='Hyderabad';

select * from employees where First_name='Thejas' and
(location='Chennai' or location='Hyderabad')

select * from employees where Experience > 2;
Similarly for Experience >= 2 , Experience < 2, Experience <= 2 ;

select * from employees where Experience BETWEEN 1 AND 3;
Operators Allowed in the WHERE Clause
EXAMPLE:
select * from employees where First_name LIKE S%;

select * from employees where First_name LIKE %a;

select * from employees where First_name LIKE G%i;

select * from employees where First_name LIKE Nith%ya;

select * from employees where First_name LIKE Ra_ _ a;

select * from employees where First_name IN (Ramya,Shiva,Gayathri);

select * from employee_details where DOJ BETWEEN '01-Feb-08' AND '01-Mar-
10';

NOTE : % indicates 0 or more characters


Operators Allowed in the WHERE Clause
ORDER BY keyword:
EXAMPLE:
select * from employees where location='Chennai' order by Emp_ID;
select * from employees where location='Chennai' order by Emp_ID desc;
select * from employees where location='Chennai' order by
Emp_ID,Experience;
select * from employees where location='Chennai' order by Experience;
Note: if the field in the table has same value then first occurrence is taken
SUBSTITUTION VARIABLES:
Select &col1,&col2 from & tablename;
DEFINE exp=3;
Select * from employees where Experience > &exp;
name cert exp
xxx 4 6
TABLE
yyy 4 5
zzz 2 4
qqq 2 4
eee
name 5
cert 8
exp
zzz 2 2
ORDER BY cert
qqq 2 4
xxx 4 6
yyy 4 5
eee
name 5
cert 8
exp
zzz 2 2
qqq 2 4 ORDER BY cert,exp
yyy 4 5
xxx 4 6
EMPLOYEE_DETAILS TABLE

Emp_ID Age Sex Skills Position DOJ Address

332197 24 Female Windows Systems 14-Dec-09 pallikaranai


Engineer

471487 22 Male Solaris Trainee 28-Feb-10 Nandambakkam

472256 22 Female Websphere Trainee 21-Feb-10 velachery

471244 35 Male MB IT Analyst 06-Jun-07 Guindy


ALIAS
Column Alias:

Select First_name as “name” from employees;

Select First_name name from employees;

Select Last_name “Sur name” from employees;

Select Last_name as “Sur name” from employees;
Table Alias

Write query to display the fields 'First_name' and 'Last_name' of
employees table and 'skills' and 'Age' field of employee_details table for
EMP_ID=332197.

We can simply the query by table Alias
select A.First_name,A.Last_name,B.Skills,B.Age from employees AS
A,employee_details AS B where Emp_ID=332197;
DEPARTMENTS TABLE

Emp_ID First_name Last_name Department Dept_ID Salary

332197 Nithya Kandappan Marketing 22 45000

333118 Gayathri P HR 22 40000

471487 Thejas Patel Support 36 30000

472256 Ramya Reddy Marketing 36 35000

501233 Shiva Kumar HR 22 60000

471244 Thejas Rajeev Support 36 45000


Predefined Functions in SQL
GROUP BY and HAVING keyword:
EXAMPLE:
select min(Salary) as “Lowest salary”from departments;
select Department,min(Salary) “Lowest salary” from departments group by Departments;
select Department,max(Salary) as “Highest salary” from departments group by
Departments;
select Department,avg(Salary) as “Average salary” from departments group by
Departments;
select Department,sum(Salary) as “Total salary” from departments group by Departments;
select count(Emp_Id) as “No. of Employees” from employees;
select city,distinct count City as “No. of Employees per city” from employees;

select Department,sum(Salary) as “Total salary” from departments group by Departments


HAVING sum(Salary)>80000 ;
INSERT INTO query
SYNTAX:

INSERT INTO table_name
VALUES (value1, value2, value3,...);

INSERT INTO table_name(col1,col2,...)
VALUES (value1, value2 ...);

INSERT INTO table_name(col1,col2,...)
Select * from table_name where condition;

Write a query to insert a new row for all the fields of employees table

Write query to insert a row which has
Emp_ID=333127 ,First_name='Aarthy' and Location='Chennai'; and
analyze what value the field Last_name will contain?????

Write a query to create a table employee_chennai which contains all the
fields and the records of employees table whose location is chennai
UPDATE query
SYNTAX:

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value;


Write a query to change the Last_name of EMP_ID 501233 to Guru

Analyze what will happen if “where” keyword is not used in UPDATE
statement.
DELETE query
SYNTAX:

DELETE FROM table_name
WHERE some_column=some_value;

DELETE FROM table_name (or) DELETE * FROM table_name

Delete is used to delete a particular record(row) from a table

Delete is also used to delete all the contents(all rows) of table

However the table structure remains
COMMIT AND ROLLBACK
COMMIT -For DML statements,explicit commit is needed. DDL statements
are auto-commited.
ROLLBACK – DML statements can be rolled back,DDL statements cannot be
rolled back.

Write a query to change the 'Skills' of emp_id 333118 to Websphere and
analyze with two users

Findings:

Concept of mutiple user logging,locks & Redo-log buffer

The values will be reflecting only in current session. If some other user
logs-in to db and queries the table, the old value will be displayed

So we need give commit manually for other users to view the changes

Since it is a DML it can be rolled backed.. but once we give commit
command, it cant be changed. Rollback is nothing but an UNDO
APPENDIX-A:EMPLOYEES TABLE
Emp_ID First_nam Last_nam Address City Experie
e e nce

332197 Nithya Kandappa pallikaranai Chennai 2


n

333118 Gayathri P Baby Nagar Chennai 2

471487 Thejas Patel Nandambakka Chennai 1


m

472256 Ramya Reddy velachery Chennai 1

501233 Shiva Kumar xxx Hyderabad 5


APPENDIX-B:EMPLOYEE_DETAILS
TABLE

Emp_ID Age Sex Skills Position DOJ Address

332197 24 Female Windows Systems 14-Dec-09 pallikaranai


Engineer

471487 22 Male Solaris Trainee 28-Feb-10 Nandambakkam

472256 22 Female Websphere Trainee 21-Feb-10 velachery


APPENDIX-C:DEPARTMENTS TABLE

Emp_ID First_name Last_name Department Dept_ID Salary

332197 Nithya Kandappan Marketing 22 45000

333118 Gayathri P HR 22 40000

471487 Thejas Patel Support 36 30000

472256 Ramya Reddy Marketing 36 35000

501233 Shiva Kumar HR 22 60000

471244 Thejas Rajeev Support 36 45000

You might also like