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

SQL

The document provides SQL commands for creating, modifying, and querying two tables: People and Employee. It includes examples of inserting, updating, deleting, and truncating records, as well as performing various types of joins between the two tables. Additionally, it demonstrates aggregate functions and sorting operations on the data within the tables.

Uploaded by

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

SQL

The document provides SQL commands for creating, modifying, and querying two tables: People and Employee. It includes examples of inserting, updating, deleting, and truncating records, as well as performing various types of joins between the two tables. Additionally, it demonstrates aggregate functions and sorting operations on the data within the tables.

Uploaded by

pushpavallik2610
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

CREATE TABLE People(

id number NOT NULL,


name varchar(45) NOT NULL,
occupation varchar(35) NOT NULL,
Age number,
primary key(id)
);
DESCRIBE People;

----insert------------

INSERT INTO People VALUES (101, 'Peter', 'Engineer', 32);

insert all into People values(102, 'Joseph', 'Developer', 30)


into People values(103, 'Mike', 'Leader', 28)
into People values(104, 'Stephen', 'Scientist', 45)
into People values(105, 'Harley', 'Designer' ,27)

Select * from People;

INSERT INTO People (id,name,occupation) VALUES (106,'Supriya','Scientist');

Select * from People; ///Without every column values

-----------Alter------
alter table People add mobile varchar(25);
select * from People;

Alter table People drop column mobile;

ALTER TABLE People


RENAME COLUMN occupation TO Work;

---------Update---------

UPDATE People
SET Age= 22
WHERE name='Supriya';

Select * from People;

---------delete----
delete from People where name = 'Harley';

----------truncate------
truncate table People;

--------drop-----
drop table People;

---------Aggregate---------

SELECT Max(salary),department FROM Employee GROUP BY department;


SELECT MIN(salary),department FROM Employee GROUP BY department;
select count(salary) from employee;
select avg(salary) from employee;
select distinct(department) from employee
--like
select * from People where name like'S_%';
select * from People where name like'%a%';

// sorting
select * from People order by age
select * from People order by age asc;
select * from employee order by salary desc;

//group by
select count(*), salary from Employee group by salary

---------------------JOINS--------------------------

CREATE TABLE Employee(


e_id int NOT NULL,
department varchar(35) NOT NULL,
salary int,
PRIMARY KEY (e_id)
);
SHOW TABLES;
DESCRIBE Employee;

INSERT INTO Employee VALUES (103, 'IT', 20000);


INSERT INTO Employee VALUES (104, 'Marketing', 30000);
INSERT INTO Employee VALUES (105, 'IT', 28000) ;
INSERT INTO Employee VALUES (106, 'Research', 45000);

Select * from Employee;

-INNER JOIN

SELECT Employee.e_id,Employee.salary, People.name


FROM Employee
INNER JOIN People ON Employee.e_id = People.id;

-LEFT JOIN

SELECT People.*, Employee.e_id


FROM People
LEFT JOIN Employee ON People.id = Employee.e_id
ORDER BY People.id;

-RIGHT JOIN

SELECT Employee.e_id, Employee.salary, Employee.department


FROM Employee
RIGHT JOIN People ON Employee.e_id = People.id
ORDER BY Employee.e_id;

-Cross JOIN

SELECT *
FROM People
CROSS JOIN Employee;
SELECT People.name, Employee.e_id
FROM People
CROSS JOIN Employee
WHERE People.id=Employee.e_id;

-self join

SELECT A.salary AS Salary1, B.salary AS salary2, A.department


FROM Employee A, Employee B
WHERE A.e_id <> B.e_id
AND A.department = B.department
ORDER BY A.department;

You might also like