SQL (Structured Query Language) is a programming language used for managing and manipulating relational databases. Here's a basic overview of its syntax:
- SELECT: Used to retrieve data from a database.
SELECT column1, column2 FROM
table_name WHERE condition;
Example
SELECT first_name, last_name FROM employees WHERE department = 'HR';
- INSERT INTO: Used to insert new records into a table.
INSERT INTO table_name (column1,
column2) VALUES (value1, value2);
Example
INSERT INTO employees (first_name, last_name) VALUES ('John', 'Doe');
- UPDATE: Used to modify existing records in a table.
UPDATE table_name SET column1 =
value1, column2 = value2 WHERE condition;
Example
UPDATE employees SET department = 'Finance' WHERE
employee_id = 101;
- DELETE: Used to delete records from a table.
DELETE FROM table_name WHERE condition;
Example
DELETE FROM employee WHERE
employee_id=102;
- CREATE TABLE: Used to create a new table in the database.
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);
Example
CREATE TABLE employees ( employee_id INT
PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), department VARCHAR(50) );
- ALTER TABLE: Used to modify an existing table.
ALTER TABLE table_name ADD column_name
datatype;
Example
ALTER TABLE employees ADD
email VARCHAR(100);
- DROP TABLE: Used to delete a table and all its data from the database.
DROP TABLE table_name;
Example
DROP TABLE table_name;
- JOIN: Used to combine rows from two or more tables based on a related column.
SELECT * FROM table1 INNER JOIN table2
ON table1.column_name = table2.column_name;
Example
SELECT
orders.order_id, customers.customer_name FROM
orders INNER JOIN
customers ON orders.customer_id = customers.customer_id;
- GROUP BY: Used to group rows that have the same values into summary rows.
SELECT column1, COUNT(column2) FROM
table_name GROUP BY column1;
Example
SELECT product_id, SUM(quantity) AS
total_quantity FROM orders GROUP BY product_id;
- ORDER BY: Used to sort the result set in ascending or descending order.
SELECT * FROM table_name ORDER BY
column_name ASC|DESC;
Example
SELECT * FROM customers ORDER BY last_name ASC;
These are some of the fundamental SQL commands and their syntax. The
exact syntax may vary slightly depending on the database management system you
are using (e.g., MySQL, PostgreSQL, SQLite, etc.), so it's always a good idea
to consult the documentation specific to your database.
Comments
Post a Comment