0% found this document useful (0 votes)
28 views2 pages

SQL ALL Hamza Nabil

The document contains SQL commands for creating databases and tables, inserting, updating, deleting and selecting data from tables. It demonstrates how to create primary and foreign keys, perform joins between tables, add and drop columns, truncate and delete tables. Comparisons are made between inner, left, right and full outer joins.

Uploaded by

amir nasser
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)
28 views2 pages

SQL ALL Hamza Nabil

The document contains SQL commands for creating databases and tables, inserting, updating, deleting and selecting data from tables. It demonstrates how to create primary and foreign keys, perform joins between tables, add and drop columns, truncate and delete tables. Comparisons are made between inner, left, right and full outer joins.

Uploaded by

amir nasser
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/ 2

--DDL

--create database Nasser


create database Nasser;
create table person
(
id int,
name varchar(50),
age int,
constraint PK_ID PRIMARY KEY(id)
);
--Drop==Delete
Drop database [Nasser]
drop table person

alter table person add city varchar(20);


alter table person drop column city; --Delete table

truncate table person; --Clean table

--DML

insert into person values(1,'Amir',20);


insert into person values(2,'Amir2',21);

update person set name ='AMir1' where id= 2;


update person set name='amir1' , age=50 where id=2;

delete from person where id =4;


delete from person; --delete all from table

select * from person;


select name from person;

select distinct name from person; --Remove the repeation of content

select id,name,age from person where age >30;

select * from person where name like '%r%' ; --%r ends with r r% starts with r %s%
s have letters before and after

--select * from table before order by


select * from person;
order by id ASC ;
order by age DESC ;

select * from person where name='rr' and age=66 ;


select * from person where name='rr' or age=20 ;
select * from person where (name='rr' and age=66) or age=21 ;
--at IN make sure that the content is found in the table.
select * from person where name IN ('rr','qq');

select * from person where id between 1 and 3 ;


select * from person where id not between 1 and 3 ;
--alias (Rename for execution only)
select age as [person age] from person ;

--primaryKey
create table Department
(
Dept_id int,
Dept_name varchar(50),
Dept_location varchar(50),
constraint PK_Dept PRIMARY KEY (Dept_id) --Primary Key important.
);

create table Employee


(
Emp_id int,
First_name varchar(50),
Last_name varchar(50),
Emp_salary varchar(50),
Dep_id int,
constraint FK_emp_dept foreign KEY (dep_id) references Department(Dept_id)
);
-----------------------------------------------------------------------------------
-
use [Amir]
go
--comparing two tables
select First_name , Dept_name from Employee , Department
where Employee.Dep_id = Department.Dept_id

--join
--inner join
select employee.first_name from employee join department
on employee.Dep_id=department.Dept_id

--left join
select employee.first_name from employee leftjoin department
on employee.Dep_id=department.Dept_id

--right join
select employee.first_name from employee rightjoin department
on employee.Dep_id=department.Dept_id

--full join
select employee.first_name from employee fulljoin department
on employee.Dep_id=department.Dept_id

You might also like