0% found this document useful (0 votes)
2 views4 pages

Basic SQL Commands

This document outlines basic SQL commands, including data retrieval, insertion, updating, and deletion operations. It also covers common clauses like ORDER BY, GROUP BY, and LIMIT, as well as various types of joins and aggregate functions. Additionally, it provides an overview of basic data types used in SQL.

Uploaded by

Aufa Syafiqa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views4 pages

Basic SQL Commands

This document outlines basic SQL commands, including data retrieval, insertion, updating, and deletion operations. It also covers common clauses like ORDER BY, GROUP BY, and LIMIT, as well as various types of joins and aggregate functions. Additionally, it provides an overview of basic data types used in SQL.

Uploaded by

Aufa Syafiqa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Basic SQL Commands

1. SELECT: Retrieve data from a table.

sql

Copy code

SELECT column1, column2 FROM table_name;

o Use * to select all columns:

sql

Copy code

SELECT * FROM table_name;

2. WHERE: Filter records based on conditions.

sql

Copy code

SELECT * FROM table_name WHERE condition;

o Example:

sql

Copy code

SELECT * FROM employees WHERE age > 30;

3. INSERT INTO: Add new rows to a table.

sql

Copy code

INSERT INTO table_name (column1, column2) VALUES (value1, value2);

4. UPDATE: Modify existing records.

sql

Copy code

UPDATE table_name SET column1 = value1 WHERE condition;

5. DELETE: Remove records from a table.

sql

Copy code

DELETE FROM table_name WHERE condition;

6. CREATE TABLE: Create a new table.

sql
Copy code

CREATE TABLE table_name (

column1 datatype,

column2 datatype

);

7. DROP TABLE: Delete a table.

sql

Copy code

DROP TABLE table_name;

8. ALTER TABLE: Modify a table's structure.

o Add a column:

sql

Copy code

ALTER TABLE table_name ADD column_name datatype;

o Drop a column:

sql

Copy code

ALTER TABLE table_name DROP COLUMN column_name;

Common Clauses

1. ORDER BY: Sort results by one or more columns.

sql

Copy code

SELECT * FROM table_name ORDER BY column_name ASC; -- Ascending (default)

SELECT * FROM table_name ORDER BY column_name DESC; -- Descending

2. GROUP BY: Group rows sharing a value.

sql

Copy code

SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name;

3. HAVING: Filter groups (used with GROUP BY).

sql
Copy code

SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name HAVING


COUNT(*) > 1;

4. LIMIT: Limit the number of returned rows.

sql

Copy code

SELECT * FROM table_name LIMIT 10;

Joins

1. INNER JOIN: Rows with matching values in both tables.

sql

Copy code

SELECT columns

FROM table1

INNER JOIN table2 ON table1.column = table2.column;

2. LEFT JOIN: All rows from the left table and matching rows from the right table.

sql

Copy code

SELECT columns

FROM table1

LEFT JOIN table2 ON table1.column = table2.column;

3. RIGHT JOIN: All rows from the right table and matching rows from the left table.

sql

Copy code

SELECT columns

FROM table1

RIGHT JOIN table2 ON table1.column = table2.column;

4. FULL JOIN: All rows when there is a match in either table.

sql

Copy code

SELECT columns
FROM table1

FULL JOIN table2 ON table1.column = table2.column;

Aggregate Functions

• COUNT(): Count rows.

sql

Copy code

SELECT COUNT(*) FROM table_name;

• SUM(): Sum values.

sql

Copy code

SELECT SUM(column_name) FROM table_name;

• AVG(): Calculate average.

sql

Copy code

SELECT AVG(column_name) FROM table_name;

• MAX() / MIN(): Get the maximum or minimum value.

sql

Copy code

SELECT MAX(column_name) FROM table_name;

Basic Data Types

• INT: Integer

• VARCHAR(size): Variable-length string

• CHAR(size): Fixed-length string

• DATE: Date (YYYY-MM-DD)

• FLOAT: Floating-point number

You might also like