2. To Create A Database:
Syntax:
Create Database {Database Name};
Purpose:
The create database query allows us to create a
database to server as a container for the tables in that
database
Query:
Create Database STUDENT;
To Use A Database:
Syntax:
{Database name};
Purpose:
Creating a database is not enough, before you create a
table you must first open the database using the Use
Database query.
Query:
Use Database STUDENT;
DROP DATABASE
Syntax:
DROP DATABASE database name;
Purpose:
If there is any unwanted database the Drop Database
query is used to remove it.
3. Query:
DROP DATABASE test DB;
To Create A Tables:
Syntax:
CREATE TABLE < Table name > (< Column name 1
> < Data type 1 >, < Column name 2 > < Data type 2
> ……., < Column name N > < Data type N >)
Purpose: Once you create a table you can store data in
it in a structured way using columns and rows.
Query:
CREATE TABLE < Table name > (< Column name 1 > <
Data type 1 >, < Column name 2 > < Data type 2 > …….,
< Column name N > < Data type N >);
OUTPUT:
Table: Student profile
S.no Register no Name Marks
1 2045 Hassan 67
2 2546 Talha 99
To Insert Into A Table:
Syntax:
Insert into { table name} Values ({ value1, value2,
valueN}) ;
Purpose:
It is used to added data in the columns of a tables.
Query:
4. Insert into STUDENT Values ( 1, 2, “Jane”, 2, 3,
“Sam”);
OUTPUT:
STUDENT
S.No Roll. No Name
1 2 Jane
2 3 Sam
Viewing Structure Of A Table:
Syntax:
Describe {table name}; / Desc { table name};
Purpose:
To view the structure of a table
Query:
Describe STUDENT; / Desc STUDENT;
OUTPUT:
STUDENT
S.No Roll. No Name
1 2 Jane
2 3 Sam
Retrieving a Column From a Table:
Syntax:
Select {column name} FROM {tablename} ;
Purpose:
This syntax is used to extract only a specific column
from a table.
Query:
5. Select Name FROM STUDENT;
OUTPUT:
STUDENT
Name
Jane
Sam
Changing The Order Of Display Of
Column:
Syntax:
Select {Column1}, {Column2}, {Column3} FROM {Table
name};
Purpose:
Allows for the rearrange of the structure of a table.
Query:
Select S. No, Name, Roll. No FROM STUDENT;
OUTPUT:
STUDENT
Changing structure of a table
a) To add a column
Syntax:
ALTER TABLE < Table name > ADD < Column name >
[ Data type];
S.No Name Roll. No
1 Jane 2
2 Sam 3
6. Purpose:
To add a column in a table, use the following
syntax:
Query:
ALTER TABLE Student profile ADD DOB Date ;
OUTPUT:
S.no Register
no
Name Marks DOB
1 4520 Hassan 23 20-05-
2000
2 5022 Talha 45 21-02-
2000
b)To drop a table
Syntax:
ALTER TABLE < Table name > DROP < Column name
> [ Data type];
Purpose:
The DROP TABLE statement is used to drop an
existing table in a database.
Query:
ALTER TABLE Student profile DROP DOB Date;
OUTPUT:
7. S.no Register no Name Marks
1 4020 Hassan 97
2 5204 Talha 86
c) To modify a column name
Syntax:
ALTER TABLE < Table name > MODIFY < Column
name > < New _ Column name >;
Purpose:
To modify a column in an existing table, the SQL
ALTER TABLE syntax is:
Query:
ALTER TABLE Student profile MODIFY Register no
Reg.no ;
OUTPUT:
S.no Reg. no Name Marks
1 8572 Hassan 94
2 7857 Talha 86
Retrieving information with SELECT
statement
I. Retrieving single column
Syntax:
8. SELECT < Column name > FROM < Table name >;
Purpose:
The SELECT statement is used to select data from a
database.
The data returned is stored in a result table, called the
result-set.
Query:
SELECT Name FROM Student profile;
OUTPUT:
Student profile
Name
Hassan
Talha
II. Retrieving multiple columns
Syntax:
SELECT < Column name 1 >, < Column name 2 >
FROM < Table name >;
Purpose:
The SELECT statement is used to select data from
a database.
The data returned is stored in a result table, called
the result-set.
9. Query:
SELECT Name , Marks FROM Student profile;
OUTPUT:
Student profile
Name Marks
Hassan 95
Talha 90
Retrieving all columns
Syntax:
SELECT * FROM < Table name >;
Query:
SELECT * FROM Student profile ;
OUTPUT:
Student profile
S.no Reg. no Name Marks
1 1123 Hassan 85
2 1234 Talha 87
Retrieving data from all rows
Using arithmetic operators with SELECT
Query:
SELECT Marks + 5 FROM Student profile;
10. OUTPUT:
Student profile
Marks + 5
90
92
Query:
SELECT Marks – 5 FROM Student profile;
OUTPUT:
Student profile
Marks - 5
80
82
Running a query without a table
SELECT 55*2 + 1;
Result : 111
Using column alias
Syntax:
SELECT < Column name > AS < “ New Column name “ >
FROM < Table name >;
Purpose:
11. SQL aliases are used to give a table, or a column in
a table, a temporary name.
Aliases are often used to make column names
more readable.
An alias only exists for the duration of the query.
Query:
SELECT Reg.no AS “ Register no “ FROM Student profile ;
OUTPUT:
Student profile
Register no
5243
1444
Putting text in query output
Query:
SELECT S.no , Reg.no , Name , ‘ Class 11B ‘ , Marks FROM
Student profile;
OUTPUT:
Student profile
S.no Reg. no Name Class 11B Marks
1 2564 Hassan Class 11B 97
2 1564 Talha Class 11B 90
12. Retrieving Specific Rows - ( Where Class):
Syntax:
Select * FROM Student WHERE { Condition};
Purpose:
This syntax is used to extract only specific rows from a
table using logical and relational operators.
Query:
SELECT * FROM Customers
WHERE Country='Mexico';
OUTPUT:
Relational operators
Syntax:
SELECT * FROM < Table name > WHERE < Column name
> < Condition >;
Query:
13. SELECT * FROM Student profile WHERE Marks = 85;
OUTPUT:
Student profile
S.no Reg. no Name Marks
1 7928 Hassan 85
Query:
SELECT * FROM Student profile WHERE Name = ‘ Talha ‘ ;
OUTPUT:
Student profile
Logical operators
Query:
SELECT * FROM Student profile WHERE Marks > 85 AND
Marks < 90;
OUTPUT:
Student profile
S.no Reg.no Name Marks
2 7629 Talha 87
S.no Reg.no Name Marks
2 1512 Talha 87
14. Condition based on range
Syntax:
SELECT * FROM < Table name > WHERE < Column name
> BETWEEN < Condition >;
Query:
SELECT * FROM Student profile WHERE Marks BETWEEN
80 AND 90 ;
OUTPUT:
Student profile
S.no Reg. no Name Marks
1 7658 Hassan 85
2 1572 Talha 87
Condition based on list
Query:
SELECT * FROM Student profile WHERE Marks IN (
85,86,87 );
OUTPUT:
Student profile
15. S.no Reg. no Name Marks
1 2316 Hassan 85
2 1210 Talha 87
Condition based on pattern matches
Syntax:
SELECT * FROM < Table name > WHERE < Column
name > LIKE PATTERN < Condition>;
Query:
SELECT * FROM Student profile WHERE Name LIKE ‘
A% ‘ ;
OUTPUT:
Student profile
S.no Reg. no Name Marks
1 3535 Hassan 85
CREATE TABLE
Syntax:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
16. );
Purpose:
The CREATE TABLE statement is used to create a
new table in a database.
Query:
CREATE TABLE Persons
(
Person ID int,
LastName varchar (255),
First Name varchar (255),
Address varchar (255),
City varchar (255)
);
Inserting into a Table
Syntax:
INSERT INTO < Table name > VALUES ( < Value 1 > , <
Value 2 > , ……,< Value N > );
Purpose:
The INSERT INTO statement is used to insert new
records in a table.
Query:
INSERT INTO Student profile VALUES ( 1 , 2052 , ‘
Hassan ‘ , 85 );
17. INSERT INTO Student profile VALUES ( 2 , 2053 , ‘
Talha ‘ , 87 );
OUTPUT:
Table: Student profile
S.no Register no Name Marks
1 2052 Hassan 85
2 2053 Talha 87
Select all the data
Syntax:
SELECT * FROM < Table name >;
Purpose:
The data returned is stored in a result table, called
the result-set.
Query:
SELECT * FROM Student profile ;
OUTPUT:
Table: Student profile
S.no Register no Name Marks
1 2052 Hassan 85
2 2053 Talha 87
18. Knowing the current database
Syntax:
Show database () / Database ();
Purpose:
SHOW DATABASES lists the databases on the Maria DB
server host. SHOW SCHEMAS is a synonym for SHOW
DATABASES.
Query:
Show database (Student profile)/Database (Student
profile)
OUTPUT:
Table: Student profile
S.no Register no Name Marks
1 2052 Hassan 85
2 2053 Talha 87
Updating Records
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Purpose:
The UPDATE statement is used to modify the
existing records in a table.
Query:
UPDATE Customers
19. SET Contact Name = 'Alfred Schmidt', City= 'Frankfurt'
WHERE Customer ID = 1;
OUTPUT:
Deleting Records
Syntax:
DELETE FROM table_name
WHERE condition;
Purpose:
The DELETE statement is used to delete existing
records in a table.
Query:
DELETE FROM Customers
WHERE Customer Name='Alfred’s Futterkiste';
OUTPUT:
20. Drop a Table
Syntax:
DROP TABLE table_name;
Purpose:
The DROP TABLE statement is used to drop an
existing table in a database.
Query:
DROP TABLE Shippers;
SELECT Column
Syntax:
SELECT * FROM _name;
Purpose:
The SELECT statement is used to select data from
a database.
The data returned is stored in a result table, called
the result-set.
Query:
SELECT * FROM Customers;
OUTPUT:
21. The SQL SELECT DISTINCT
Statement
Syntax:
SELECT DISTINCT <column1, column2, ...>
FROM table_name;
Purpose:
The SELECT DISTINCT statement is used to return
only distinct (different) values.
Query:
SELECT DISTINCT Country FROM Customers;
OUTPUT:
The SQL WHERE Clause
WHERE
22. Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Purpose:
The WHERE clause is used to filter records.
The WHERE clause is used to extract only those
records that fulfill a specified condition.
Query:
SELECT * FROM Customers
WHERE Country='Mexico';
OUTPUT:
The SQL AND, OR and NOT
Operators
AND
Syntax:
SELECT column1, column2, ...
FROM table_name
23. WHERE condition1 AND condition2 AND condition
3 ...;
Purpose:
The AND operator displays a record if all the
conditions separated by AND is TRUE.
Query:
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
OR
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3
...;
Purpose:
The OR operator displays a record if any of the
conditions separated by OR is TRUE.
Query:
SELECT * FROM Customers
WHERE City='Berlin' OR City='Munched';
NOT
Syntax:
24. SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
Purpose:
The NOT operator displays a record if the
condition(s) is NOT TRUE.
Query:
SELECT * FROM Customers
WHERE NOT Country='Germany';
OUTPUT:
The SQL ORDER BY Keyword
Syntax:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Purpose:
The ORDER BY keyword is used to sort the result-
set in ascending or descending order.
25. The ORDER BY keyword sorts the records in
ascending order by default. To sort the records in
descending order, use the DESC keyword.
Query:
SELECT * FROM Customers
ORDER BY Country ASC, Customer Name DESC;
OUTPUT:
MIN ()
Syntax:
SELECT MIN(column_name)
FROM table_name
WHERE condition;
Purpose:
The MIN() function returns the smallest value of
the selected column.
Query:
SELECT MIN(Price) AS Smallest Price
FROM Products;
OUTPUT:
26. MAX ()
Syntax:
SELECT MAX(column_name)
FROM table_name
WHERE condition;
Purpose:
The MAX () function returns the largest value of
the selected column.
Query:
SELECT MAX(Price) AS Largest Price
FROM Products;
OUTPUT:
LIKE
27. Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
Purpose:
The LIKE operator is used in a WHERE clause to
search for a specified pattern in a column.
There are two wildcards used in conjunction with
the LIKE operator:
% - The percent sign represents zero, one, or
multiple characters
_ - The underscore represents a single
character
Query:
SELECT * FROM Customers
WHERE Customer Name LIKE 'a%';
OUTPUT:
BETWEEN
28. Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Purpose:
The BETWEEN operator selects values within a
given range. The values can be numbers, text, or
dates.
The BETWEEN operator is inclusive: begin and end
values are included.
Query:
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
OUTPUT:
SELECT INTO
Syntax:
Copy all columns into a new table:
SELECT *
INTO new table [IN external dB]
29. FROM old table
WHERE condition;
Purpose:
The SELECT INTO statement copies data from one
table into a new table.
Query:
SELECT * INTO Customers Germany
FROM Customers
WHERE Country = 'Germany';
INSERT INTO SELECT
Syntax:
Copy all columns from one table to another
table:
INSERT INTO table2
SELECT * FROM table1
WHERE condition;
Purpose:
The INSERT INTO SELECT statement copies data
from one table and inserts it into another table.
INSERT INTO SELECT requires that data types
in source and target tables match
The existing records in the target table are
unaffected
30. Query:
INSERT INTO Customers (Customer Name, Contact
Name, Address, City, PostalCode,Country)
SELECT Supplier Name, Contact Name, Address,
City, Postal Code, Country FROM Suppliers;
OUTPUT:
GROUP BY
Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Query
31. SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;
DROP TABLE
Syntax:
DROP TABLE table_name;
Purpose:
The DROP TABLE statement is used to drop an
existing table in a database.
Query:
DROP TABLE Shippers;
OUTPUT:
SQL Arithmetic Operators