SQL Queries
SQL Queries
Syntax:
Create database database-name;
Example:
Create database school1;
Syntax:
Use database-name;
Example:
Use school1;
Delete database:
Syntax:
DROP DATABASE database-name;
Example:
DROP DATABASE school1;
Create table:
Syntax:
CREATE TABLE table_name (column1 datatype (range), column2 datatype (range),
column3 datatype (range), .... column “n” datatype(range));
Example:
Create table student1 (Sid int (5), sname varchar (25));
Delete table:
Syntax:
DROP TABLE table_name;
Example:
Drop table Student1;
INSERT VALUES IN A DATABASE TABLE AND
SELECT A TABLE COMMAND IN SQL
Insert into table:
Syntax:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2,
value3, ...);
Example:
INSERT INTO Student1 (Sid, Sname) VALUES (02, ‘Vikas’);
In all columns
If we wanted to add values in all the columns of the table, then no need to mention any
column name, just pass the values.
Syntax:
INSERT INTO table_name VALUES (value1, value2, value3, ...);
Example:
INSERT INTO Student1 VALUES (02, ‘Vikas’);
Please note that in the above statement the order of the values should be followed as per the
column structure.
Select a table:
Syntax:
SELECT column1, column2, ... FROM table_name;
Example:
Select Sid, Sname from student1;
Syntax:
Select*from table-name;
Example:
Select*from student1;
Selects distinct values only:
Avoid repeated values
Syntax:
Select distinct column_name from table_name;
Example:
Select distinct Sid from student1;
ALTER AND UPDATE COMMAND IN SQL
To add new column
Syntax:
ALTER TABLE table_name ADD column_name datatype(data range);
Example:
ALTER TABLE student1 ADD city varchar(15);
Example:
ALTER TABLE student1 Drop column city;
Example:
ALTER TABLE student1 Modify city char(20);
Example:
ALTER TABLE student1 Modify city char(20) after name;
Example:
ALTER TABLE student1 change city Town varchar(15);
To rename a table
Syntax:
ALTER TABLE existing_table_name rename new_table_name;
Example:
ALTER TABLE student1 rename stud1;
UPDATE COMMAND
To update a table
Syntax:
Update table_name set column_name= ‘New value’;
Example:
Update student1 set town= ‘Alwar’;
Example:
Update student1 set town= ‘jaipur’ where sid= ‘01’;
Example:
Update student1 set town= ‘jaipur’ where sid= ‘01’;
Example:
Update student1 set town= ‘Tonk’, class= ‘10’ where sid= ‘01’;
WHERE CLAUSE
Equal
To select a row or a record of one entity
Syntax
Select*from table_name where column_name = ‘value’;
Example:
Select*from student1 where sid= ‘01’;
Greater than
Syntax
Select*from table_name where column_name > ‘value’;
Example:
Select*from student1 where sid > ‘01’;
less than
Syntax
Select*from table_name where column_name < ‘value’;
Example:
Select*from student1 where sid < ‘01’;
Example:
Select*from student1 where sid >= ‘01’;
Example:
Select*from student1 where sid <= ‘01’;
Not equal
Syntax
Select*from table_name where column_name <> ‘value’;
Example:
Select*from student1 where sid <> ‘01’;
Between
Syntax
Select*from table_name where column_name between ‘value1 and value 2’;
Example:
Select*from student1 where sid between ‘1 and 2’;
In
Syntax
Select*from table_name where column_name in (‘value1, value 2’);
Example:
Select*from student1 where city in (‘Tonk’, ‘Jaipur’);
DELETE, DROP AND TRUNCATE IN SQL
Delete
Drop
To drop a column
Syntax:
Alter table table_name drop column_name;
Example:
Alter table student1 drop town;
Syntax:
Drop database database_name;
Example:
Drop database school1;
Truncate table
Syntax:
Truncate table table_name;
Example:
Truncate table student1;
NOT NULL CONSTRAINT IN SQL
While create table:
Syntax:
Create table table_name (column1 datatype not null, column2 datatype(range));
Example:
Create table student2(sid int not null, name varchar(5));
Example:
Alter table persons Modify Age int Not Null;
UNIQUE CONSTRAINT IN SQL
Example:
Create table student2 (id int not null, name varchar (15), unique (id));
In middle of a query
Syntax:
Create table table_name (column1 datatype not null unique, column2 datatype (range));
Example:
Create table student2 (id int not null unique, name varchar (15));
Example:
Create table student2 (id int not null, name varchar (15), constraint UC_student2 unique
(id,name));
In middle of a query
Syntax:
Create table table_name (column1 datatype not null unique, column2 datatype (range)
unique);
Example:
Create table student2 (id int not null unique, name varchar (15) unique);
Example:
Alter table student2 Add Unique (Sid);
Drop Unique constraint from an existing table
Syntax:
Alter table table_name drop constraint column_name;
Example:
Alter table student2 drop constraint sid;
PRIMARY KEY IN SQL
Example:
CREATE TABLE Std_pk (sid int NOT NULL primary key, name varchar (30));
Example:
CREATE TABLE Std_pk (sid int NOT NULL,name varchar(30), PRIMARY KEY (sid));
Example:
CREATE TABLE Std_pk (sid int NOT NULL,name varchar(30),
PRIMARY KEY (sid,name));
Example:
Alter table Std_pk add primary key (sid);
Example:
Alter table Std_pk add primary key (Sid, name);
Drop a primary key
Syntax:
Alter table table_name drop primary key;
Example:
Alter table Std_pk drop primary key;
FOREIGN KEY IN SQL
Syntax:
Create table table_name (column1 datatype(range), column2 datatype(range), foreign key
(column_name) references primary key table_name (primary key column_name));
Example:
create table std_fk (id int, city varchar (15), foreign key (id) references std_pk (sid));
Syntax:
Alter table table_name add foreign key (column_name) references primary key table_name
(primary key column_name);
Example:
Alter table stdfk add foreign key (sid) references std_pk(sid);
syntax
Show create table table_name;
Example:
Show create table stdfk;
After this query the code block of the table is shown. Find out the foreign key constraint
name.
Now apply this constraint name with Alter table query.
Syntax:
Alter table table_name drop foreign key constraint_name;
Example:
Alter table stdfk drop foreign key stdfk_ibfk_1;
After follow this whole procedure we can remove the foreign key from a table.
I personally suggest that please watch the video.
INDEX
1. Order By Command
2. Auto-Increment Command
3. SQL Index Constraint
4. And, Or, Not Operators In SQL
5. SQL Limit Clause, Min & Max Function
6. Count, Sum & Avg Function In SQL
7. SQL In, Between Operator
8. Like Operator In SQL
9. Alias And Group by Clause In SQL
10. Group By
11. All, Any/Some Operators In SQL
ORDER BY COMMAND
Simple order
Syntax:
Select*from table_name order by column_name;
Example:
Select*from s_index order by age;
In Descending order
Syntax:
Select*from table_name order by column_name desc;
Example:
Select*from s_index order by age desc;
CREATE INDEX
Syntax:
CREATE INDEX index_name ON table_name (column1, column2, ...);
Example:
CREATE INDEX index_city ON std1 (city);
AND, OR, NOT OPERATORS IN SQL
And Operator
Select all columns which satisfy the condition
Syntax:
SELECT * FROM table_name WHERE condition 1 AND condition 2;
Example:
SELECT * FROM s_index WHERE city='Jaipur' AND Sid='7';
Or Operator
Select all columns which satisfy the condition
Syntax:
SELECT * FROM table_name WHERE condition 1 or condition 2;
Example:
SELECT * FROM s_index WHERE city='Jaipur' or Sid='7';
And-Or Operator
Select all columns which satisfy the condition
Syntax:
SELECT * FROM table_name WHERE condition 1 And (condition2 or condition 3);
Example:
SELECT * FROM s_index WHERE age='16' and (city=’Jaipur’ or city=’patna’);
Maximum
Syntax:
SELECT MAX(column_name) As temporary_view_name FROM table_name;
Example:
SELECT MAX(age) As maximum_age FROM std1;
Minimum
Syntax:
SELECT Min(column_name) As temporary_view_name FROM table_name;
Example:
SELECT Min(age) As minimum_age FROM std1;
COUNT, SUM & AVG FUNCTION IN SQL
Between Operator
To select specific values
Syntax:
SELECT*FROM table_name WHERE column_name Between value1 and value2;
Example:
SELECT*FROM s_index WHERE age between ‘14’ and ’16’;
Here is the list of all LIKE operators with '%' and '_' wildcards:
SOME
Syntax:
SELECT ColumnX_name from table_name where columnY_name operator some (select
columnY_name from table_name where condition);
Example:
SELECT sname from std5 where percentage_in_last_class > some (select
percentage_in_last_class from std5 where class = 10);
ALL
Use a subquery to execute main query.
Use Boolean function.
Execute the main query if all of the subquery values meet the condition.
Syntax:
SELECT ColumnX_name from table_name where columnY_name operator all (select
columnY_name from table_name where condition);
Example:
SELECT sname from std5 where percentage_in_last_class > all (select
percentage_in_last_class from std5 where class = 10);
INDEX
1. Case statement in SQL
2. Insert into select statement in SQL
3. Copy data from one table to another
4. SQL view statement & create table from another table
5. Having clause in SQL
6. Exists clause in SQL
7. Join in SQL | types of joins | inner join
8. Left join, right join & full outer join in SQL
9. Right join
10. Full outer/ outer join
11. Cross join
12. Natural join
13. Self join
CASE STATEMENT IN SQL
Syntax:
Select column_name
Case
when condition1 then result1
when condition2 then result2
when conditionn then resultn
else result
End as alias_name
From table_name;
Example:
Select sname
Case
when percentage_in_last_class>70 then 1st_division
else 2nd_division
End as division
From std5;
INSERT INTO SELECT STATEMENT IN SQL
COPY DATA FROM ONE TABLE TO ANOTHER
For all data
Syntax:
Insert into table2 select * from table1 where condition;
Example:
Insert into std6 select * from student where class=10;
Show view
Syntax:
Select*from view_name;
Example:
Select*from class_10;
Show view
Syntax:
Drop view view_name;
Example:
Drop view class_10;
Example:
Select std1.sid, name, bid, bname from std1 inner join library on std1.sid = library.sid;
Example:
Select*from std1 inner join library on std1.sid = library.sid;
LEFT JOIN, RIGHT JOIN & FULL OUTER JOIN IN SQL
LEFT JOIN
SELECTED COLUMN
Syntax:
Select column_name from table1 left join table2 on table1.column_name =
table2.column_name;
Example:
Select std1.sid, name, bid, bname from std1 left join library on std1.sid = library.sid;
Example:
Select*from std1 left join library on std1.sid = library.sid;
RIGHT JOIN
SELECTED COLUMN
Syntax:
Select column_name from table1 right join table2 on table1.column_name =
table2.column_name;
Example:
Select std1.sid, name, bid, bname from std1 right join library on std1.sid = library.sid;
Example:
Select std1.sid, name, bid, bname from std1 LEFT join library on std1.sid = library.sid
UNION
Select std1.sid, name, bid, bname from std1 right join library on std1.sid = library.sid;