Professional Documents
Culture Documents
SQL Queries
SQL Queries
----------------------------------------------------------
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
----------------------------------------------------------
--------------------------------------------------------------------------
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)
--------------------------------------------------------------------------
7. Deleting the table
--------------------------------------------------------------------------
----------------------------------------------------------
8. Select statement
----------------------------------------------------------
----------------------------------------------------------
9. Select statement with selected columns
----------------------------------------------------------
----------------------------------------------------------
10. Select statement with a condition (numerical operators)
----------------------------------------------------------
select * from student1 where age <22
----------------------------------------------------------
11. Select statement with a condition (String operators)
----------------------------------------------------------
select * from student1 where firstname = "Megha"