You are on page 1of 3

SQL Queries Create Command

----------------------------------------------------------
1. Simple table creation
----------------------------------------------------------
CREATE TABLE person (ID int,
FirstName varchar(255),
LastName varchar(255),
Age int)
----------------------------------------------------------
2. Simple table creation
----------------------------------------------------------
CREATE TABLE student (
EnrollNo char(15),
FirstName char(20),
LastName char(20),
Email char(50),
Age int)

----------------------------------------------------------
3. Table creation with constraints
----------------------------------------------------------
CREATE TABLE student1 (
EnrollNo char(15) PRIMARY KEY,
FirstName char(20) NOT NULL,
LastName char(20),
Email char(50) UNIQUE,
Age int)

----------------------------------------------------------
4. Table creation with Foreign Key
----------------------------------------------------------

CREATE TABLE course(


CourseNo char(15) PRIMARY KEY,
CourseName char(20) NOT NULL,
EnrollmentNo char(15) references student1(EnrollNo),
coursecredit int,
sem int)

--------------------------------------------------------------------------
5. Inserting default value / data validation while inserting
--------------------------------------------------------------------------
CREATE TABLE student2 (
EnrollNo char(15) PRIMARY KEY,
FirstName char(20) NOT NULL,
LastName char(20),
Email char(50) UNIQUE,
Age int check (Age>0),
City char(50) DEFAULT "Delhi"
)

----------------------------------------------------------
6. Inserting new record into table
----------------------------------------------------------
Insert into student1 (EnrollNo, FirstName, LastName, Email, Age)
values ("20BSP1234", "Neha","Sharma","neha.sharma@gmail.com", 20)

Insert into student1 (EnrollNo, FirstName, LastName, Email, Age)


values ("20BSP1235", "Megha","Sharma","megha.sharma@gmail.com", 22)

Insert into student1


values ("20BSP1236", "Richa","Sharma","richa.sharma@gmail.com", 23)

Insert into student1


values ("20BSP1238", "Richica","Sharma","richica.sharma@gmail.com", 23),
("20BSP1237", "Abhay","Sharma","abhay.sharma@gmail.com", 23)

--------------------------------------------------------------------------
7. Deleting the table
--------------------------------------------------------------------------

drop table student

----------------------------------------------------------
8. Select statement
----------------------------------------------------------

select * from student1

----------------------------------------------------------
9. Select statement with selected columns
----------------------------------------------------------

select EnrollNo, FirstName, Age from student1

----------------------------------------------------------
10. Select statement with a condition (numerical operators)
----------------------------------------------------------
select * from student1 where age <22

SELECT * FROM OrderDetails where productid = 11

SELECT * FROM OrderDetails where productid between 11 and 15

SELECT * FROM OrderDetails where productid not between 11 and 15

SELECT * FROM OrderDetails where productid in (11,12,13)

SELECT * FROM OrderDetails where productid not in (11,12,13)

----------------------------------------------------------
11. Select statement with a condition (String operators)
----------------------------------------------------------
select * from student1 where firstname = "Megha"

select * from student1 where firstname = "megha"

select * from student1 where firstname Like "megha"

select * from student1 where firstname like "%e%"

select * from student1 where firstname like "__e%"


select * from student1 where firstname not like "__e%"

select * from student1 where firstname in ("sneha", "nikita")

You might also like