You are on page 1of 8

Learning SQL

SQL structured query language

Databases:

• Oracle SQL

• MS SQL Server SQL

• My SQL SQL

• DB2 SQL

• Hbase No-SQL

• Mongo DB No-SQL

1.Structured(SQL) / Relational databases(RDBMS)

The databases which stores the data in the form of tables

Table contains rows(Records) and columns

SQL structured query language

SQL is used to manage data in a relational database system.

What is Relational database?

A relational database, also called Relational Database Management System (RDBMS) or SQL
database, stores data in tables (i.e. structured format in rows and columns) and rows also
referred to as records
Many times, the data within these tables have relationships with one another, or dependencies.

2.Non Structured(No-SQL) / Non Relational Databases

These databases stores data in the form of documents

A non relational database is document-oriented, meaning, all information gets stored in more
of a laundry list order.
SQL categorized as

1.DDL Data definition language

DDL consists of the SQL commands like

• Create creating table

• Drop Drop the table (Removing the table)

• Alter is used to alter the table definition [ex: add or remove columns]

• Truncate To remove the data from table

• Rename To rename Table Name or column names

2.DQL Data Query Language

DQL consists of

• Select – to query/read the data from table

3 DML Data Manipulation Language

DML consists of

• Insert -- > To create new record/row in a table

• Update To update the existing records/rows in table

• Delete To delete the record/row in table

4.DCL Data Control Language

5.TCL Transactional Control Language

We are going to learn Oracle SQL

We will install Oracle Database in our computer

DQL Data Query Language(DQL):

Sele query is used to view data from one or more tables

Format
> select <column_Names> from <Table_Name> [Filters] ;

<column_Names> ==> you can give one or more columns with coulmns separated by comma

-- Column name are not case sensitive [columns can give in uppercase,Lower case or sentence case]

-- To retrive data from all columns ,You can use * instead of column names

-- * mean all

-- <Table_Name > is not case sensitive

Comments:

Any command or query starts with -- will be treated as comment in SQL

SELECT:

-- To select the data from only required columns Ex: EMPNO,ENAME and Job from EMP table

select empno,ENAME,JOb from Emp;

-- To retirve data from all columns in a table

select * from emp;

-- To view the column names as custom columns

select EMPNO as Employee_Number from EMP;

-- How to use the custom columns having space in between

-- Use double quotes for the words tosend/treat as a single string

select EMPNO as "Employee Number" from EMP;

-- Using arthemetic operations iin select statement

-- For example if you want to calculate the yearly salary for an employee

--If you are using arthemetic operations you should not use asteric (*) in select statement

select ename,sal from emp;

select ename,sal as Montly_Sal,(sal*12) as Yearly_Sal from emp;

select ename,sal as Montly_Sal,(sal*12) as Yearly_Sal,comm,(12*sal*0.1) as TAX, (12*sal*12)-


(12*sal*0.1) as "Net Salary" from emp;
-- How to calculate experience for an employee in EMP table

select hiredate,sysdate from emp;

select sysdate-hiredate as experience from emp;

select ename,hiredate,sysdate, floor(months_between(sysdate, hiredate)/12) as yearsofexperience


from emp;

1.DISTINCT:

It is used to retiveonly distinc/uniquet values from table

It can be used on one column or multiple columns

Ex: Customer Table

CustId CustName

100 Ram

101 Nithin

101 Sam

103 Vinay

103 Vinay

104 kiran

> select distinct CustId from Customer;

Output:

100

101

103

104

> select distinct CustId,CustName from Customer;

OutPut:

100 Ram

101 Nithin

101 Sam

103 Vinay

104 kiran
2.Where:

Where clause is used to filer the data from table

select <Column1>,<Column2>,<Column3> from <Table_Name> where <Column1> = Condition

Ex: You have EMP and you want to retieve the employeess from only from DEPTNO 10

select * from EMP where deptno = 10;

IN SQL if you are string you need enclose within single quotes.

select * from EMP where job = 'MANAGER';

3.AND Operator:

AND Operator deplays a record in all the conditions TRUE which are used in AND

TRUE AND TRUE = TRUE

TRUE AND False = False

False AND False = False

False AND True = False

-- select managers from deptNo 10

select * from emp where job = 'MANAGER' AND deptno = 10;

4. OR Operator:

OR deplys record if atleast one condition is TRUE

TRUE OR TRUE = TRUE

TRUE OR False = TRUE

False OR TRUE = True

False OR False = False

-- select managers or emplyees from deptno 10;

select * from emp where job = 'MANAGER' OR deptno=10;

5.NOT Operator:

it deplays inverted out for the given condition


NOT True = False

NOT False = TRUE

-- Show all emplyess except deptno 10;

select * from emp where deptno != 10;

Metadata:

it is data about the data

Fro example if you see in a table the table names and column names are metadata

because these gives the information about the table and data in the columns

Metadata is case insensitive(Not sensitive)

Data:

It is the information present in the table

the data in the table is case sensitive

so while given the data for string it is manadatory to enclose in the single quotes and give the string with
exact case.

Ex:

select * from emp where job = 'MANAGER'; -- Correct query

select * from emp where job = 'Manager'; -- wrong query

6. IN Operator:

The IN operator allows you to specify multiple values in a where clause.

For Example if you want data from DEPTNO 10 and DEPTNO 20

Ex:

select * from emp where DEPTNO = 10 OR DEPTNO = 20;

select * from EMP where DEPTNO IN (10,20);

LIKE Operator:

Like operator is used in where clause to search for a specific pattern in a column.

select * from EMP where ename like 'J%';


WILDCard symbols:

In Oracle SQL we have 2 wild card symbols

%(Percentage) -- It represents zero,one or more characters

_(Underscore) -- It represents exactly one character

select * from EMP where ename like 'J_';

select * from EMP where ename like 'J%';

LIKE Operator Description

WHERE CustomerName LIKE 'a%' Finds any values that start with "a"

WHERE CustomerName LIKE '%a' Finds any values that end with "a"

WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position

WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position

WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2 characters
in length

WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3 characters
in length

WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"

To insert data into EMP table:

insert into emp (empno,ename,job,mgr,hiredate,sal,deptno) values (100,'JC','MANAGER',1001,'12-APR-


95',5000,10);

insert into emp (empno,ename,job,mgr,hiredate,sal,deptno) values (101,'J','MANAGER',1002,'12-APR-


95', 6000,30);

commit;

Order by

Order by keyword is used to sort the results in Ascending or Descending order

Ex:
select column1,column2,column3 from emp where condition order by <column1>
,<column2>,<column3> ASC| DESC;

-- select the data from EMP table and sort the data in asscending order on EMPNo;

select * from EMP order by empno;

select * from EMP order by empno ASC;

-- -- select the data from EMP table and sort the data in descending order on EMPNO;

select * from EMP order by empno DESC;

-- -- select the data from EMP table and sort the data in asscending order on EMPNo and ENAME and
JOb;

select * from EMP order by empno;

select DEPTNO,ENAME,EMPNO from EMP order by DEPTNO,ENAME,EMPNO DESC;

Order by always to be mentioned at the end of query

You might also like