SQL COMMANDS
Database: Container that holds schemas,
tables, data and other database objects
Schema: Like a Namespace in Database.
Logically groups all the tables, views, stored
procedures and functions. Assume like a folder
in a database
Different Data Types:
1. INT
2. FLOAT
3. CHAR
4. VARCHAR
5. DATE
6. TIME
7. DATETIME
8. BOOLEAN
1. Create Database: CREATE DATABASE <db_name>
2. Drop Database: DROP DATABASE <db_name>
3. Create Schema: CREATE SCHEMA <db_name>
4. Drop Schema: DROP SCHEMA <db_name>
5. Create the database if it does not exist in the system: CREATE DATABASE
IF NOT EXISTS <db_name>;
6. Use Database: use <db_name>;
7. Create Table: CREATE TABLE <table_name> ( <column_name> <data_type>,
<another_column_name> <data_type>, ……);
8. Inserting row of Data: INSERT INTO <table_name> (column_name1,
column_name2,...........) VALUES (value1, value2, …………..);
9. Inserting rows of Data: INSERT INTO <table_name> (column_name1,
column_name2,...........) VALUES (value1, value2, …………..),(value1, value2,
…………..),(value1, value2, …………..)
10. Primary key plays an important role which provides a unique reference for every
record. It should be specified while creating the table. Only one primary key can
be present for a table. Primary Key cannot have null values
Adding a Primary key during table creation:
CREATE table emp_details (
id INT (AUTO_INCREMENT), (Auto_Increment is used to increase the value of
ID by 1 automatically)
name varchar(50),
department varchar(50),
age INT,
PRIMARY KEY(id)
);
11. Different Constraints in SQL:
● NOT NULL → Data for that particular column should be not be a null
value
● CHECK → Checks if you want to accept the data for the particular column
based on the condition (Syntax : CHECK (age>=18)
● DEFAULT → If the value is constant for one column, then instead of
mentioning it in the insert query all the time, we can use this DEFAULT
keyword to assign a common value. (Syntax: DEFAULT ‘value’)
● UNIQUE → Ensures that the data in that particular column is unique at all
times, similar to Primary Key but primary key can be applied on one
column for a table. This can be used for more than one column.
12. Fetching Records from Table: select * from <table_name> (* means we are
retrieving the data from all the columns)
13. Fetching Records from Table with alias column names: select COL_NAME
as “<desired_column_name>”, COL_NAME2 as “<desired_column_name>” from
<table_name> (* means we are retrieving the data from all the columns)
14. Fetching Records from Table with where conditions: select * from
<table_name> where COL_NAME1=<value> (Along with the equal operator we can use
all the logical operators required to perform the operations).
15. Fetching Records from Table with multiple where conditions: select * from
<table_name> where COL_NAME1=<value> and COL_NAME=<value2> (In this case
both the conditions should be satisfied)
select * from <table_name> where COL_NAME1=<value> or
COL_NAME=<value2> (In this case either of the conditions should be satisfied)
select * from <table_name> where NOT( COL_NAME1=<value>) and
COL_NAME=<value2> (In this case both the conditions should be satisfied and NOT
states the inverse of the given condition)
16. In Operator: It is used to reduce the length of the query and makes it more
concise. We can reduce the number of OR Conditions.
select * from <table_name> where column_name in (value_1, value_2);
17. Between Operator: Helps us in fetching the records between a range of data
for particular columns
select * from <table_name> where column_name between value_1 and
value_2
18. Order By: Helps us in sorting the data in ascending or descending order based
on a particular column name. (By default SQL Sorts the data in ascending order).
select * from <table_name> order by <column_name>
select * from <table_name> where <column_name_1> = <value> order by
<column_name>
19. Distinct: Helps us in fetching the unique records from the table
select DISTINCT <column_name>,* from <table_name>
20. Updating the records of the table:
update <table_name> set <column_name> = <value1>, <column_name>
= <value2>, … where <column_name_filter> = <value_filter>,....
21.