Sei sulla pagina 1di 6

SELECT

******
select * from table_name
select column_name from table_name
DISTINCT
********
select distinct column_name from table_name
WHERE
*****
select * from table_name where condition
AND,OR
******
select sales from store_information where sales>1000 or(sales<500 and sales>275)
IN
**
select sales from store_information where store_name in('San Diego','Boston')
BETWEEN AND
***********
select sales from store_information where sales between 500 and 2000
WILDCARD
********
select * from store_information where store_name like '%Die%'
select * from store_information where store_name like '_oston'
ORDER BY
********
select * from store_information order by sales asc
select * from store_information order by sales desc
AGGREGATE FUNCTION
******************
select avg(sales) from store_information
select sum(sales) from store_information
select max(sales) from store_information
select min(sales) from store_information
select count(sales) from store_information
GROUP BY
********
select store_name,sum(sales) from store_information group by store_name
GROUP BY,HAVING
***************
select store_name,sum(sales) from store_information group by store_name having s
um(sales)>1000
ALIAS
*****
select sum(sales) sum_sales from store_information
select sum(sales) "sum_sales" from store_information
select sum(sales) as "sum_sales" from store_information
AS
**
select a1.store_name,a2.region_name from store_information as a1,geography as a2
where a1.store_name=a2.store_name
select a1.store_name,a2.sales from geography as a1,store_information as a2 where
a1.store_name=a2.store_name(+)
UNIQUE
******
select unique sales from store_information
CONCAT
******
select concat(store_name,region_name) from geography
select store_name || ' ' || 'IN' || region_name from geography
SUBSTRING
*********
select substring('Rajalakshmi',3,5) from dual
TRIM
****
select trim(' Rajalakshmi ') from dual
select ltrim(' Rajalakshmi') from dual
select rtrim('Rajalakshmi ') from dual
LENGTH
******
select length('Rajalakshmi) from dual
REPLACE
*******
select replace('Rajalakshmi','Raja','Vara') from dual
SYSDATE
*******
select sysdate from dual
CREATE TABLE
************
create table store_information
(store_name char(25),
sales integer)
CONSTRAINTS
***********
create table store_information
(store_name char(25),
sales integer NOT NULL)
create table store_information
(store_name char(25) UNIQUE,
sales integer)
create table store_information
(store_name char(25),
sales integer CHECK(sales>0))
create table store_information
(store_name char(25),
sales integer DEFAULT 1000)
create table store_information
(store_name char(25) PRIMARY KEY,
sales integer)
create table geography
(region char(25),
st_name char(25) references store_information(store_name))
VIEW
****
create view vw_store_information as select store_name from store_information
INDEX
*****
create index ind_store_information on store_information(store_name)
ALTER
*****
alter table store_information add country char(25)
alter table store_information modify country char(15)
alter table store_information rename column country to country_name
alter table store_information drop column country
alter table store_information add index ind_stname(store_name)
alter table store_information drop index ind_stname
alter table store_information add constraint con_uni unique(store_name)
alter table store_information add primary key(store_name)
alter table geography add constraint con_stname foreign_key(st_name) references
store_information(store_name)
alter table geography drop constraint con_stname
DROP
****
drop table geography
TRUNCATE
********
truncate table geography
INSERT INTO
***********
insert into store_information values ('XXX',57783)
insert into store_information(store_name,sales) values ('YYY',43534)
UPDATE
******
update table store_information set store_name='YYY' where store_name='XXX'
DELETE
******
delete from store_information where store_name='YYY'
UNION
*****
select store_name from store_information
UNION
select st_name from geography
UNION ALL
*********
select store_name from store-information
UNION ALL
select st_name from geography
INTERSECT
*********
select store_name from store_information
INTERSECT
select st_name from geography
MINUS
*****
select store_name from store_information
MINUS
select st_name from geography
SUB QUERY
*********
select store_name from store_information where store_name in (select st_name whe
re region='EAST')
CORRELATED SUB QUERY
********************
select a.store_name from store_information a where a.store_name in (select b.st_
name where b.st_name=a.store_name)
EXISTS
******
select store_name from store_information where EXISTS (select st_name where regi
on='EAST')
CASE
====
select store_name,
CASE store_name when 'XXX' then sales*1.2
when 'YYY' then sales*2.2
else sales
END "inc_sales"
from store_information
NVL
***
select SUM(NVL(sales,1000)) from store_information
COALESCE
********
select name,COALESCE(buss_phone,mobile_phone,home_phone) from user_details
NULLIF
******
select name,NULLIF(actual,goal) from user_details
RANK
****
select a1.name,a1.sales,count(a2.sales) sales_rank
FROM total_sales a1,total_sales a2
WHERE a1.sales<=a2.sales OR (a1.sales=a2.sales AND a1.name=a2.name)
GROUP BY a1.sales,a1.name
ORDER BY a1.sales DESC,a1.name DESC
MEDIAN
******
select sales
FROM
(select a1.name,a1.sales,count(a2.sales) rank
from total_sales a1,total_sales a2
WHERE a1.sales<=a2.sales OR (a1.sales=a2.sales AND a1.name=a2.name)
GROUP BY a1.sales,a1.name
ORDER BY a1.sales DESC,a1.name DESC) a3
where rank=(select (count(*)+1) DIV 2 from total_sales)
RUNNING TOTAL
*************
select a1.name,a1.sales,sum(a2.sales) Runn_sales
from total_sales a1,total_sales a2
where a1.sales<=a2.sales OR (a1.sales=a2.sales AND a1.name=a2.name)
GROUP BY a1.sales,a1.name
ORDER BY a1.sales DESC,a1.name DESC
PERCENT TO TOTAL
****************
select a1.name,a1.sales,a1.sales/(select sum(sales) from total_sales) per_total
from total_sales a1,total_sales a2
where a1.sales<a2.sales OR (a1.sales=a2.sales AND a1.name=a2.name)
GROUP BY a1.sales,a1.name
ORDER BY a1.sales DESC,a1.name DESC
CUMULATIVE PERCENT TO TOTAL
***************************
select a1.name,a1.sales,sum(a2.sales)/(select sum(sales) from total_sales) cum_p
er_total
from total_sales a1,total_sales a1
where a1.sales<=a2.sales OR (a1.sales=a2.sales AND a1.name=a2.name)
GROUP BY a1.sales,a1.name
ORDER BY a1.sales DESC,a1.name DESC

Potrebbero piacerti anche