Uploaded by angelo.natalizia

SQL Commands

advertisement
SQL Commands
SELECT
Retrieves data from one or more database tables.
Syntax: SELECT column1, column2, ... FROM table_name WHERE condition;
FROM
Specifies the table or tables from which to retrieve data.
Syntax: SELECT column1, column2, ... FROM table_name WHERE condition;
JOIN
Combines rows from two or more tables based on a related column between
them.
Syntax: SELECT column1, column2, ... FROM table1 JOIN table2 ON
table1.column = table2.column;
WHERE
Filters rows based on a specified condition.
Syntax: SELECT column1, column2, ... FROM table_name WHERE condition;
ORDER
BY
Sorts the result set based on one or more columns in ascending (ASC) or
descending (DESC) order.
Syntax: SELECT column1, column2, ... FROM table_name ORDER BY
column_name ASC/DESC;
INSERT
INTO
Inserts new rows into a table.
Syntax: INSERT INTO table_name (column1, column2, ...) VALUES (value1,
value2, ...);
COUNT
Returns the number of rows that match a specified condition.
Syntax: SELECT COUNT(column) FROM table_name WHERE condition;
LIKE
Searches for a specified pattern in a column using wildcard characters (% and
_).
Syntax: SELECT column1, column2, ... FROM table_name WHERE
column_name LIKE 'pattern';
GROUP
BY
Groups rows based on specified columns and allows applying aggregate functions on
each group.
Syntax: SELECT column1, column2, ... FROM table_name GROUP BY column1,
column2, ...; For example, consider a table called Orders with columns CustomerID,
ProductID, and Quantity. If you want to calculate the total quantity of products ordered
by each customer, you can use the following query: SELECT CustomerID,
SUM(Quantity) AS TotalQuantity FROM Orders GROUP BY CustomerID;
SUM
Calculates the sum of values in a column.
Syntax: SELECT SUM(column) FROM table_name;
Returns the maximum value of a column.
Syntax: SELECT MAX(column) FROM table_name;
MAX
Example: SELECT OrderID, TotalAmount FROM Orders
WHERE TotalAmount = (SELECT MAX(TotalAmount) FROM Orders);
MIN
AND/OR
Returns the minimum value of a column.
Syntax: SELECT MIN(column) FROM table_name;
Specify multiple conditions (AND both have to be true, OR one or the other)
Syntax: SELECT * FROM Customers WHERE Country = 'USA' AND Age > 30;
Syntax: SELECT * FROM Customers
WHERE (Country = 'USA' AND Age > 30) OR (Country = 'Canada' AND Age >
40);
Download