Structured Query Language (SQL) is a powerful tool used for managing and manipulating data in a relational database. There are many SQL commands, but some are more important than others.
In this article, we will discuss the most important SQL commands that you need to know.
- SELECT → retrieves data from a table or view
- INSERT → adds new rows to a table
- UPDATE → modifies existing rows in a table.
- DELETE → removes rows from a table.
- CREATE → creates a new database object such as a table, view, or index.
- ALTER → modifies the structure of an existing database object
- DROP → deletes a database object, such as a table or view
- TRUNCATE → removes all rows from a table
- JOIN → combines data from two or more tables
- GROUP BY → groups of rows based on the values in one or more columns
- ORDER BY → sorts the result set by one or more columns
- HAVING → filters the result set based on aggregate values
- DISTINCT → eliminates duplicate rows from the result set
- LIKE → filters the result set based on a pattern match
- UNION → combines the result sets of two or more SELECT statements
1. SELECT
SELECT is one of the most commonly used SQL commands. It is used to retrieve data from a table or view. With SELECT, you can specify the columns you want to retrieve and the table or view from which you want to retrieve data. SELECT also allows you to filter the result set using WHERE, GROUP BY, HAVING, and ORDER BY clauses.
SELECT column1, column2 FROM table WHERE condition;
2. INSERT
INSERT is used to add new rows to a table. With INSERT, you can specify the values you want to insert into each column of the table. You can also use INSERT to insert data from another table.
INSERT INTO table (column1, column2) VALUES (value1, value2);
3. UPDATE
UPDATE is used to modify existing rows in a table. With UPDATE, you can specify the columns you want to update and the new values you want to set for those columns. You can also use UPDATE to update rows based on a condition.
UPDATE table SET column1 = value1 WHERE condition;
4. DELETE
DELETE is used to remove rows from a table. With DELETE, you can specify the rows you want to remove based on a condition.
DELETE FROM table WHERE condition;
5. CREATE
CREATE is used to create a new database object such as a table, view, or index. With CREATE, you can specify the name of the new object, the columns it will contain, and any constraints that should be enforced.
CREATE TABLE table (column1 datatype1, column2 datatype2, …);
6. ALTER
ALTER is used to modify the structure of an existing database object. With ALTER, you can add, modify, or remove columns from a table, change the data type of a column, or add or remove constraints.
ALTER TABLE table ADD column datatype;
7. DROP
DROP is used to delete a database object, such as a table or view. With DROP, you can specify the name of the object you want to delete.
DROP TABLE table;
8. TRUNCATE
TRUNCATE is used to remove all rows from a table. With TRUNCATE, you can specify the table you want to truncate.
TRUNCATE TABLE table;
9. JOIN
JOIN is used to combine data from two or more tables. With JOIN, you can specify the columns that the tables have in common and the type of join you want to perform (e.g. INNER JOIN, LEFT JOIN, RIGHT JOIN).
SELECT column1, column2 FROM table1 JOIN table2 ON table1.column = table2.column;
10. GROUP BY
GROUP BY is used to group rows based on the values in one or more columns. With GROUP BY, you can specify the columns you want to group by and any aggregate functions you want to apply to the grouped data.
SELECT column1, COUNT(*) FROM table GROUP BY column1;
11. ORDER BY
ORDER BY is used to sort the result set by one or more columns. With ORDER BY, you can specify the columns you want to sort by and the order in which you want to sort the data (e.g. ASC for ascending order, DESC for descending order).
SELECT column1, column2 FROM table_name ORDER BY column1, column2 ASC;
12. HAVING
HAVING is used to filter the result set based on aggregate values. With HAVING, you can specify the conditions that the aggregate values must meet.
SELECT column1, COUNT(*) FROM table GROUP BY column1 HAVING COUNT(*) > 1;
13. DISTINCT
DISTINCT is used to eliminate duplicate rows from the result set. With DISTINCT, you can specify the columns that you want to use to determine whether a row is unique or not.
SELECT DISTINCT column1, column2 FROM table;
14. LIKE
LIKE is used to filter the result set based on a pattern match. With LIKE, you can specify a pattern using wildcards (% and _), and SQL will return all rows that match that pattern.
SELECT column1, column2 FROM table WHERE column1 LIKE ‘%pattern%’;
15. UNION
UNION is used to combine the result sets of two or more SELECT statements. With UNION, you can specify the columns that the SELECT statements have in common, and SQL will return all rows from each SELECT statement.
SELECT column1, column2 FROM table1
UNION
SELECT column1, column2 FROM table2;
In conclusion, these SQL commands are the most important ones that you need to know. They are used to retrieve, add, modify, and delete data in a relational database. By mastering these commands, you can easily manage and manipulate data in your database.
You may also like:- How To Get Image Information in PHP
- Integrating Elasticsearch with Node.js – A Complete Guide
- 22 Useful JavaScript Functions You Need To Know
- CSS3 nth-child Selector – A Comprehensive Guide
- PHP Loops – A Comprehensive Guide
- Different Types of Functions in PHP
- Various String Types in PHP – Utilizing ctype Functions
- Understanding Conditional Statements in PHP
- Mastering PHP Arrays – A Comprehensive Guide
- Exploring Strings in PHP – A Comprehensive Guide