- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL Syntax
SQL (Structured Query Language) is used to interact with databases, allowing users to query, insert, update, and manage data. Below are key SQL components and their basic syntax.
Basic SQL Commands
SELECT - Retrieve data from a table.
SELECT column1, column2 FROM table_name WHERE condition;
INSERT INTO - Add new rows to a table.
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
UPDATE - Modify existing data.
UPDATE table_name SET column1 = value1 WHERE condition;
DELETE - Remove data from a table.
DELETE FROM table_name WHERE condition;
CREATE TABLE - Define a new table.
CREATE TABLE table_name (column1 datatype, column2 datatype);
ALTER TABLE - Modify a table structure.
ALTER TABLE table_name ADD column_name datatype;
DROP TABLE - Delete a table.
DROP TABLE table_name;
Clauses
WHERE: Filters records based on conditions.
SELECT * FROM table_name WHERE condition;
ORDER BY: Sorts records.
SELECT * FROM table_name ORDER BY column_name;
GROUP BY: Groups rows by a specified column.
SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name;
JOIN: Combines rows from two or more tables.
SELECT * FROM table1 JOIN table2 ON table1.column_name = table2.column_name;
Functions
COUNT(): Counts the number of rows.
SELECT COUNT(*) FROM table_name;
SUM(): Adds values in a numeric column.
SELECT SUM(column_name) FROM table_name;
AVG(): Returns the average value.
SELECT AVG(column_name) FROM table_name;
Constraints
- PRIMARY KEY: Ensures unique records.
- FOREIGN KEY: Defines relationships between tables.
- NOT NULL: Ensures a column cannot contain NULL values.
- UNIQUE: Ensures all values in a column are distinct.
Summary
SQL syntax consists of commands, clauses, and functions used for querying, updating, and managing data in a relational database. Mastering these basics allows effective interaction with databases.