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

Coding 1

Uploaded by

sunil.babu101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Coding 1

Uploaded by

sunil.babu101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

select * from customers;

desc customers;

CREATE TABLE customers


( customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
city varchar2(50),
CONSTRAINT customers_pk PRIMARY KEY (customer_id)
);

CREATE TABLE employees


( employee_id number(10) NOT NULL,
employee_name varchar2(50) NOT NULL,
zone varchar2(50),
CONSTRAINT employees_pk PRIMARY KEY (employee_id)
);

INSERT INTO table


(column1, column2, ... column_n )
VALUES
(expression1, expression2, ... expression_n );

INSERT INTO employees


(employee_id, employee_name, zone)
VALUES
(4, 'Asad Ali', 'Karachi');

insert into test1


(id, name, city, age, salary, designation, department)
values
(1, 'Salman Khan', 'Karachi', 48, 560000, 'Chief Technology Officer', 'NOC +
Software');

SQL> insert into employees (employee_id,employee_name,zone) values


(&enter_no,'&enter_name','&enter_city');
Enter value for enter_no: 11
Enter value for enter_name: adil
Enter value for enter_city: khi
old 1: insert into employees (employee_id,employee_name,zone) values
(&enter_no,'&enter_name','&enter_city')
new 1: insert into employees (employee_id,employee_name,zone) values
(11,'adil','khi')

Insert all
INTO employees (employee_id, employee_name, zone) VALUES (5,'Muhammad
Salahuddin', 'Karachi')
INTO employees (employee_id, employee_name, zone) VALUES (6,'Wajid Saleem',
'Karachi')
INTO employees (employee_id, employee_name, zone) VALUES (7,'Muhammad Waqas',
'Karachi')
INTO employees (employee_id, employee_name, zone) VALUES (8,'Adil Ibrahim',
'Karachi')
SELECT * FROM dual;

Insert all
INTO employees (employee_id, employee_name, zone) VALUES (9,'Muhammad Imtiaz',
'Islamabad')
INTO employees (employee_id, employee_name, zone) VALUES (10,'Affan',
'Islamabad')
SELECT * FROM dual;

select * from employees where employee_id in (11,12);

delete from employees where employee_id in (11,12);

ALTER TABLE employees


ADD (employee_position varchar2(50),
marital_status varchar2(50));

update employees set emp_age=36 where employee_id=1;

UPDATE suppliers
SET supplier_address = 'Agra',
supplier_name = 'Bata shoes'
WHERE supplier_id = 1;

UPDATE employees
SET salary =
CASE
WHEN employee_id=1 THEN '800 USD'
WHEN employee_id=2 THEN '358 USD'
END
where employee_id in (1,2);

UPDATE employees
SET salary =
CASE
WHEN employee_id=5 THEN '1260 USD'
WHEN employee_id=6 THEN '620 USD'
WHEN employee_id=7 THEN '790 USD'
WHEN employee_id=8 THEN '630 USD'
WHEN employee_id=9 THEN '1400 USD'
WHEN employee_id=10 THEN '916 USD'
END
where employee_id in (5,6,7,8,9,10);

SELECT *
FROM employees
ORDER BY emp_age ASC

SELECT *
FROM employees
ORDER BY emp_age DESC
select employee_name from employees order by emp_age desc

select employee_name from employees order by emp_age asc

select * from employees;

UPDATE employees
SET salaries =
CASE
WHEN employee_id=1 THEN 250000
WHEN employee_id=2 THEN 99000
WHEN employee_id=3 THEN 167000
WHEN employee_id=4 THEN 170000
WHEN employee_id=5 THEN 350000
WHEN employee_id=6 THEN 185000
WHEN employee_id=7 THEN 198000
WHEN employee_id=8 THEN 188000
WHEN employee_id=9 THEN 430000
WHEN employee_id=10 THEN 280000
END
where employee_id in (1,2,3,4,5,6,7,8,9,10);

insert into test


values (5, 'Shakeel Ahmed', 'Lahore', 45, 170000, 'Team Lead Central', 'NOC');
insert into test
values (6, 'Sumair Syed', 'Karachi', 35, 220000, 'Senior Team Leader', 'NOC');

update test
set age =
CASE
when id=1 then 49
when id=2 then 45
END
where id in (1,2);

update customers set name='bob', city='london' where id=101;

delete from customers where id=101;

select employee_name, emp_age, salaries


from employees1
where emp_age > 22
AND salaries < 200000
order by salaries desc;

select test.name, employees1.emp_age


from employees1
INNER JOIN test
ON employees1.employee_id = employee_id
order by name;
insert into test1
(id, name, city, age)
select employee_id, employee_name, zone, emp_age
from employees1
where salaries > 180000;

insert all
into test values (7, 'Adeel Khan', 'Karachi', 33, 140000, 'Technical Engineer
North', 'NOC')
into test values (8, 'Talha Siddiqui', 'Karachi', 35, 130000, 'Technical Engineer
South', 'NOC')
into test values (9, 'Adeel Hussain', 'Karachi', 37, 120000, 'Technical Engineer
DRS', 'NOC')
select * from dual;

select count(*) from test where age > 40;

INSERT ALL
INTO test values (10, 'Riaz Uddin', 'Karachi', 31, 150000, 'Software Development
Lead', 'Software')
INTO employees values (11, 'Dummy', 'Karachi', 'desig random', '500 USD')
select * from dual;

update employees
set emp_name = 'randomz'
where emp_id = 11;

UPDATE employees
SET emp_name = 'Jigsaw',
city = 'Badar',
salary = '450 USD'
WHERE emp_id = 11;

delete from employees


where emp_name = 'Jigsaw';

select distinct designation


from test
where name = 'Zeeshan';

select distinct name, age, salary


from test
where name = 'Zeeshan';

SELECT employees.emp_id, employees.emp_name, employees.city, test.age


from employees
inner JOIN test
ON employees.emp_id = test.id;
SELECT test.id, test.name, test.city, test.age, employees.salary
from test
INNER JOIN employees
ON test.id = employees.emp_id;

select * from test order by age age;

select * from test order by salary desc;

SELECT department,
MIN(salary) AS "Lowest salary"
FROM test
GROUP BY department;

SELECT department,
MAX(salary) AS "Highest salary"
FROM test
GROUP BY department;

SELECT department,
MIN(age) AS "Youngest"
FROM test
GROUP BY department;

SELECT department,
MAX(age) AS "Eldest"
FROM test
GROUP BY department;

SELECT department,
SUM(salary) AS "Total salaries"
FROM test
GROUP BY department;

SELECT department,
COUNT(*) AS "Number of employees"
FROM test
WHERE salary > 200000
GROUP BY department;

SELECT department,
SUM(salary) AS "Total salaries"
FROM test
GROUP BY department
HAVING SUM(salary) >500000;

SELECT department,
COUNT(*) AS "Number of employees"
FROM test
WHERE salary > 2000
GROUP BY department
HAVING COUNT(*) > = 2;

SELECT department,
MIN(salary) AS "Lowest salary"
FROM test
GROUP BY department
HAVING MIN(salary) > 160000;

SELECT department,
MAX(age) AS "Eldest"
FROM test
GROUP BY department
HAVING MAX(age) < 48;

select name
from test
union
select emp_name
from employees;

select id
from test
union
select emp_id
from employees;

create table test3


AS (select * from test);

SELECT test3.id, test3.name, test.age


FROM test3
INNER JOIN test
ON test3.id = test.id;

SELECT test.id, test.name, test3.age


FROM test
LEFT OUTER JOIN test3
ON test.id = test3.id;

SELECT test3.id, test3.name, test.age


FROM test3
LEFT OUTER JOIN test
ON test3.id = test.id;

SELECT test.id, test.name, test3.age


FROM test3
RIGHT OUTER JOIN test
ON test3.id = test.id;
SELECT test.id, test.name, test3.age
FROM test3
FULL OUTER JOIN test
ON test3.id = test.id;

SELECT test.city, test3.name, test3.salary


FROM test, test3
WHERE test.id = test3.id;

SELECT join1.city, join3.emp_name, join3.designation


FROM join1, join3
WHERE join1.id = join3.emp_id;

SELECT a.name, b.age, a.SALARY


FROM test a, test b
WHERE a.SALARY < b.SALARY;

SELECT *
FROM join1
CROSS JOIN join2;

SELECT * FROM join1, join2;

You might also like