SQL (Structured Query Language) is a powerful tool for managing and querying databases. Let’s review some essential concepts and commands.
Selecting All Columns: The asterisk (*) is used with the SELECT statement to select all columns from a table.
COUNT(DISTINCT) Usage: The COUNT(DISTINCT Country) expression returns the total number of unique values in the “Country” column.
UPDATE Command without WHERE Clause: An UPDATE statement without a WHERE clause will update all records in the specified table.
LIKE Operator with Wildcard: The ‘%a’ pattern in the LIKE operator matches employees with EmpName ending in the letter “a.”
Aggregate Function Example: COUNT(), AVG(), and SUM() are all examples of aggregate functions in SQL.
DROP DATABASE Command: The DROP DATABASE command will delete the specified database and all its contents.
Creating a New Table from Another Table: The CREATE TABLE … AS SELECT … statement is used to create a new table from the result of a SELECT query.
Effects of DELETE Statement: The DELETE statement removes existing records from the specified table.
Constraints and Primary Keys: Primary keys must be unique and not null to uniquely identify each record.
Adding a Column to an Existing Table: The ALTER command is used to modify the structure of an existing table, including adding columns.
KeyWords To Take Note Of:
Data Retrieval:SQL’s core purpose is to retrieve data from databases, and the primary keyword for this task is SELECT
. This keyword is used to specify the columns you want to retrieve data from and the table from which to retrieve it. The FROM
keyword is used to indicate the source table. To filter the results based on specific conditions, the WHERE
keyword is employed.
Data Manipulation:Adding, modifying, and deleting data are essential aspects of database management. The INSERT
keyword allows you to add new rows to a table, while the UPDATE
keyword enables you to modify existing data. To remove data, you can use the DELETE
keyword, and the TRUNCATE
keyword deletes all rows from a table without removing the table structure.
Aggregate Functions: SQL provides several aggregate functions that perform calculations on a set of values. The COUNT
function counts the number of rows, while SUM
, AVG
, MIN
, and MAX
compute the sum, average, minimum, and maximum of a column, respectively. These functions are especially useful for data analysis.
Joining Tables: To combine data from multiple tables, the JOIN
keyword is used. Different types of joins include:
INNER JOIN
: Returns matching rows from both tables.LEFT JOIN
: Returns all rows from the left table and matching rows from the right table.RIGHT JOIN
: Returns all rows from the right table and matching rows from the left table.FULL OUTER JOIN
: Returns all rows from both tables.
Sorting and Grouping: The ORDER BY
keyword is employed to sort query results in ascending or descending order. The GROUP BY
keyword groups rows based on the values of a specified column. Additionally, the HAVING
keyword filters the grouped rows based on specific conditions.
String Manipulation: For string concatenation, the ||
operator is used. SQL also supports pattern matching using the LIKE
operator and wildcard characters (%
and _
).
Subqueries and EXISTS: Subqueries are queries embedded within other queries. The EXISTS
keyword is used to test the existence of a subquery result. The BETWEEN
operator checks if a value is within a specified range
Mastering these essential SQL keywords and functions is vital for effective database management. Whether you’re retrieving, manipulating, or analyzing data, these tools provide a solid foundation for working with databases. By understanding and using these concepts, you’ll be well-equipped to interact with databases and gain valuable insights from your data.