0% found this document useful (0 votes)
1 views22 pages

SQL Queries

The document provides an overview of SQL statements used for database management, including creating, altering, and deleting databases and tables. It includes syntax and examples for various SQL commands such as CREATE, DROP, INSERT, SELECT, UPDATE, and DELETE. Additionally, it covers the use of constraints and aggregate functions in SQL queries.

Uploaded by

gachanepal246
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views22 pages

SQL Queries

The document provides an overview of SQL statements used for database management, including creating, altering, and deleting databases and tables. It includes syntax and examples for various SQL commands such as CREATE, DROP, INSERT, SELECT, UPDATE, and DELETE. Additionally, it covers the use of constraints and aggregate functions in SQL queries.

Uploaded by

gachanepal246
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

SQL Queries :

SQL Syntax
1. CREATE DATABASE Statement
This SQL statement creates the new database in the database management system.

• Syntax of CREATE DATABASE Statement:


CREATE DATABASE database_name;
• Example of CREATE DATABASE Statement:
CREATE DATABASE Company;

2. DROP DATABASE Statement


This SQL statement deletes the existing database with all the data tables and views from the
database management system.

• Syntax of DROP DATABASE Statement:


DROP DATABASE database_name;
• Example of DROP DATABASE Statement:
DROP DATABASE Company;
3. CREATE TABLE Statement
This SQL statement creates the new table in the SQL database.
• Syntax of CREATE TABLE Statement:
CREATE TABLE table_name
(
column_name1 data_type [column1 constraint(s)],
column_name2 data_type [column2 constraint(s)],
.....
…..,
PRIMARY KEY(one or more col)
);
• Example of CREATE TABLE Statement:
CREATE TABLE Employee_details
(
Emp_Id NUMBER(4) NOT NULL,
First_name VARCHAR(30),
Last_name VARCHAR(30),
Salary Money,
City VARCHAR(30),
PRIMARY KEY (Emp_Id)
);

This example creates the table Employee_details with five columns or fields in the SQL database. The fields in the table are Emp_Id, First_Name,
Last_Name, Salary, and City. The Emp_Id column in the table acts as a primary key, which means that the Emp_Id column cannot contain duplicate
values and null values.
4. ALTER TABLE Statement
This SQL statement adds, deletes, and modifies the columns of the table in the SQL database.

Syntax of ALTER TABLE Statement:

ALTER TABLE table_name ADD column_name datatype[(size)];


The above SQL alter statement adds the column with its datatype in the existing database table.

ALTER TABLE table_name MODIFY column_name column_datatype[(size)];


The above 'SQL alter statement' renames the old column name to the new column name of the existing database table.

ALTER TABLE table_name DROP COLUMN column_name;


The above SQL alter statement deletes the column of the existing database table.

ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;

Example of ALTER TABLE Statement:


ALTER TABLE Employee_details
ADD Designation VARCHAR(18);

This example adds the new field whose name is Designation with size 18 in the Employee_details table of the SQL
database.
5. INSERT INTO Statement
This SQL statement inserts the data or records in the existing table of the SQL database. This
statement can easily insert single and multiple records in a single query statement.

• Syntax of insert a single record:


INSERT INTO table_name
( column_name1, column_name2, .…, column_nameN )
VALUES
(value_1, value_2, ..…, value_N );

• Example of insert a single record:


INSERT INTO Employee_details
( Emp_ID, First_name, Last_name, Salary, City )
VALUES
(101, Akhil, Sharma, 40000, Bangalore );
• Syntax of inserting a multiple records in a single query:

INSERT INTO table_name


( column_name1, column_name2, .…, column_nameN)
VALUES (value_1, value_2, ..…, value_N),
(value_1, value_2, ..…, value_N),…….;

• Example of inserting multiple records in a single query:

INSERT INTO Employee_details


( Emp_ID, First_name, Last_name, Salary, City )
VALUES (101, Amit, Gupta, 50000, Mumbai),
(101, John, Aggarwal, 45000, Calcutta),
(101, Sidhu, Arora, 55000, Mumbai);
6. DROP TABLE Statement
This SQL statement deletes or removes the table and the structure, views,
permissions, and triggers associated with that table.

• Syntax of DROP TABLE Statement:


DROP TABLE [ IF EXISTS ]
table_name1, table_name2, ……, table_nameN;
The above syntax of the drop statement deletes specified tables completely if they
exist in the database.

• Example of DROP TABLE Statement:


DROP TABLE Employee_details;

This example drops the Employee_details table if it exists in the SQL database.
This removes the complete information if available in the table.
7. TRUNCATE TABLE Statement
This SQL statement deletes all the stored records from the table of the SQL
database.

• Syntax of TRUNCATE TABLE Statement:


TRUNCATE TABLE table_name;

• Example of TRUNCATE TABLE Statement:


TRUNCATE TABLE Employee_details;

This example deletes the record of all employees from the Employee_details
table of the database.
8. SELECT Statement:
This SQL statement reads the data from the SQL database and shows it as the output to the database
user.

Syntax of SELECT Statement:


SELECT column_name1, column_name2, .…, column_nameN
FROM table_name
WHERE condition
ORDER BY order_column_name1 [ ASC | DESC ], .... ;

• Example of SELECT Statement:


SELECT Emp_ID, First_Name, Last_Name, Salary, City
FROM Employee_details
WHERE Salary = 100000
ORDER BY Last_Name ;

This example shows the Emp_ID, First_Name, Last_Name, Salary, and City of those employees from
the Employee_details table whose Salary is 100000. The output shows all the specified details
according to the ascending alphabetical order of Last_Name.
9. UPDATE Statement
This SQL statement changes or modifies the stored data in the SQL database.

• Syntax of UPDATE Statement:

UPDATE table_name
SET column_name1 = new_value_1,...., column_nameN = new_value_N
WHERE CONDITION ;

• Example of UPDATE Statement:


UPDATE Employee_details
SET Salary = 100000
WHERE Emp_ID = 10;

This example changes the Salary of those employees of the Employee_details table
whose Emp_ID is 10 in the table.
10. DELETE Statement
This SQL statement deletes the stored data from the SQL database.

• Syntax of DELETE Statement:


DELETE FROM table_name
WHERE CONDITION ;

• Example of DELETE Statement:


DELETE FROM Employee_details
WHERE First_Name = 'Sumit’;

This example deletes the record of those employees from


the Employee_details table whose First_Name is Sumit in the table.
1. ADD:

• Select all records of table customers where the City column has the value
'Berlin' and the PostalCode column has the value '12209’.

Select * FROM Customers where City = 'Berlin’ And Country= '12209';

2. OR:

• Select all records where the City column has the value 'Berlin’ or ’ london ’.

Select * FROM Customers where City = 'Berlin’ or City = ‘London’;

One Example using IN (Multiple or)


SELECT * FROM Customers where country IN ('France’, ‘Norway’);

3. NOT

• Use the NOT keyword to select all records where City is NOT "Berlin".

SELECT * FROM Customers where not city= ‘berlin';


• Select all records where the value of the City column starts with the letter "a".

SELECT * FROM Customers ;

• Select all records where the value of the City column ends with the letter "a".

SELECT * FROM Customers ;

• Select all records where the value of the City column starts with the letter "a“ and
ends with “d”.

SELECT * FROM Customers ;

• Select all records where the value of the City column that contains the letter "a".

SELECT * FROM Customers ;

• Select all records where the value of the City column that doesnot starts with the
letter "a".

SELECT * FROM Customers ;


• Select all records where the second letter of the City is an
"a".

SELECT * FROM Customers WHERE City LIKE ‘ ';


• Select all records where the first letter of the City is an "a" or a "c" or an "s".

SELECT * FROM Customers WHERE City LIKE ‘ ';

• Select all records where the first letter of the City starts with anything from an "a" to
an "f".

SELECT * FROM Customers WHERE City LIKE ‘ ';

• Select all records where the first letter of the City is NOT an "a" or a "c" or an "f".

SELECT * FROM Customers WHERE City LIKE ‘ ';


•Min

•Max

•Count

•Avg

•Sum

•As
1. a)Create a table named library with columns book_id (integer, primary key), book_title
(varchar, 100), author (varchar, 50),publish_date (date).
b) Add a new column Pages (integer) to the library table.
c) Delete the library table from the database.

a) CREATE TABLE library b) ALTER TABLE library


c)DROP TABLE library;
( ADD pages INT;
book_id INT PRIMARY KEY,
book_title VARCHAR(100),
author VARCHAR(50),
publish_date DATE
);
2. a)Create a table named students with columns student_id (integer, primary key),
student_name (varchar, 100), enrollment_date (date), and major (varchar, 50).
b) Add a new column email (varchar, 150) to the students table.
c) Drop the students table from the database.

a) CREATE TABLE students


( student_id INTEGER PRIMARY KEY,
student_name VARCHAR(100),
enrollment_date DATE,
major VARCHAR(50)
);

b) ALTER TABLE students


ADD COLUMN email VARCHAR(150);

c) DROP TABLE students;


1. Create a Table: a) Create a table named employees with columns employee_id (integer, primary key),name
(varchar, 100), position (varchar, 50), and hire_date (date).

2. Add a Column: b) Add a new column salary (decimal) to the employees table.

3. Update Data: c) Update the position of the employee with employee_id 5 to "Senior Developer".

4. Select Data: d) Select all columns from the employees table where the hire_date is after January 1, 2020.

5. Delete Data: e) Delete all employees from the employees table where the salary is less than 30000.

6. Truncate Table: f) Truncate the employees table. (Note: This will remove all rows but keep the table structure.)

7. Create a Table with Constraints: g) Create a table named orders with columns order_id (integer, primary
key),customer_id (integer), order_date (date), and total_amount (decimal). Ensure that total_amount cannot be
negative.

8. Select with Aggregate Functions: h) Select the average total_amount from the orders table where order_date is
in the year 2023.

9. Add a Not Null Constraint: i) Alter the orders table to add a constraint that the customer_id column cannot be null.

10. Drop Table: j) Drop the orders table from the database.
1. CREATE TABLE employees ( employee_id INTEGER PRIMARY KEY, name
VARCHAR(100), position VARCHAR(50), hire_date DATE );

2. ALTER TABLE employees ADD COLUMN salary DECIMAL(10, 2);

3. UPDATE employees SET position = 'Senior Developer' WHERE


employee_id = 5;

4. SELECT * FROM employees WHERE hire_date > '2020-01-01’;

5. DELETE FROM employees WHERE salary < 30000;


6. TRUNCATE TABLE employees;

7. CREATE TABLE orders ( order_id INTEGER PRIMARY KEY, customer_id


INTEGER, order_date DATE, total_amount DECIMAL(10, 2) CHECK
(total_amount >= 0) );

8. SELECT AVG(total_amount) AS average_amount FROM orders WHERE


YEAR(order_date) = 2023;

9. ALTER TABLE orders MODIFY customer_id INTEGER NOT NULL;

10. DROP TABLE orders;


Thank you

You might also like