0% found this document useful (0 votes)
11 views

SQL Queries

Uploaded by

areebadilshad2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

SQL Queries

Uploaded by

areebadilshad2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL Queries

1. Data Definition Language (DDL) Queries


DDL queries define and modify the structure of a database and
its objects.
Create Operations
 Create a Database:
CREATE DATABASE database_name;
 Create a Table:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);
 Create an Index:

CREATE INDEX index_name ON table_name (column_name);


Alter Operations
 Add a Column to a Table:

ALTER TABLE table_name ADD column_name datatype;


 Modify a Column in a Table:

ALTER TABLE table_name MODIFY column_name new_datatype;


 Drop a Column from a Table:

ALTER TABLE table_name DROP COLUMN column_name;


Drop Operations
 Drop a Database:

DROP DATABASE database_name;


 Drop a Table:

DROP TABLE table_name;


 Drop an Index:

DROP INDEX index_name ON table_name;

2. Data Manipulation Language (DML) Queries


DML queries are used to manipulate data within tables.
Insert Operations
 Insert Data into a Table:
INSERT INTO table_name (column1, column2, ...) VALUES
(value1, value2, ...);
 Insert Multiple Rows:

INSERT INTO table_name (column1, column2, ...) VALUES


(value1, value2, ...),
(value3, value4, ...);
Update Operations
 Update Existing Data:

UPDATE table_name SET column1 = value1, column2 = value2


WHERE condition;
Delete Operations
 Delete Specific Rows:

DELETE FROM table_name WHERE condition;


 Delete All Rows:

DELETE FROM table_name;

3. Data Query Language (DQL) Queries


DQL queries are used to retrieve data from tables.
Select Operations
 Retrieve All Data:

SELECT * FROM table_name;


 Retrieve Specific Columns:

SELECT column1, column2 FROM table_name;


 Use Aliases:

SELECT column_name AS alias_name FROM table_name;


 Filter Data:

SELECT * FROM table_name WHERE condition;


 Sort Data:

SELECT * FROM table_name ORDER BY column_name


ASC/DESC;
 Limit Results:
SELECT * FROM table_name LIMIT number_of_rows;

4. Aggregate Queries
Aggregate functions perform calculations on a set of rows.
 Count Rows:

SELECT COUNT(*) FROM table_name;


 Sum Values:

SELECT SUM(column_name) FROM table_name;


 Average Values:

SELECT AVG(column_name) FROM table_name;


 Find Minimum and Maximum Values:

SELECT MIN(column_name), MAX(column_name) FROM


table_name;
 Group Data:

SELECT column_name, COUNT(*) FROM table_name GROUP BY


column_name;

5. Joins
Joins combine data from multiple tables.
Inner Join
 Retrieve Matching Rows from Both Tables:
sql
Copy code
SELECT * FROM table1
INNER JOIN table2 ON table1.column_name =
table2.column_name;
Left Join
 Retrieve All Rows from the Left Table:

SELECT * FROM table1


LEFT JOIN table2 ON table1.column_name =
table2.column_name;
Right Join
 Retrieve All Rows from the Right Table:

SELECT * FROM table1


RIGHT JOIN table2 ON table1.column_name =
table2.column_name;
Full Join
 Retrieve All Rows from Both Tables:

SELECT * FROM table1


FULL OUTER JOIN table2 ON table1.column_name =
table2.column_name;
Cross Join
 Cartesian Product of Both Tables:

SELECT * FROM table1 CROSS JOIN table2;

6. Constraints
Constraints enforce rules on data.
 Add a Primary Key:

ALTER TABLE table_name ADD PRIMARY KEY (column_name);


 Add a Foreign Key:

ALTER TABLE table_name ADD FOREIGN KEY (column_name)


REFERENCES other_table(column_name);
 Add a Unique Constraint:

ALTER TABLE table_name ADD UNIQUE (column_name);

7. Views
Views are virtual tables based on the result of a query.
 Create a View:

CREATE VIEW view_name AS


SELECT column1, column2 FROM table_name WHERE condition;
 Use a View:

SELECT * FROM view_name;


 Drop a View:

DROP VIEW view_name;

8. Stored Procedures
Stored procedures are reusable SQL code blocks.
 Create a Stored Procedure:

CREATE PROCEDURE procedure_name AS


BEGIN
SQL_STATEMENTS;
END;
 Call a Stored Procedure:

EXEC procedure_name;

9. Triggers
Triggers automatically perform actions when certain events
occur.
 Create a Trigger:

CREATE TRIGGER trigger_name


AFTER INSERT ON table_name
FOR EACH ROW
BEGIN
SQL_STATEMENTS;
END;
 Drop a Trigger:
DROP TRIGGER trigger_name;

10. Transactions
Transactions are used to group multiple queries into a single
logical unit.
 Begin Transaction:

BEGIN TRANSACTION;
 Commit Changes:

COMMIT;
 Rollback Changes:
ROLLBACK;

You might also like