Basic Data Retrieval
The SELECT statement is used to fetch data from a database. This query retrieves two specific columns: first_name and last_name from the employees table.
Open snippet →Read these Beginner SQL snippets line by line — each one comes with a written breakdown of what the code does and why.
The SELECT statement is used to fetch data from a database. This query retrieves two specific columns: first_name and last_name from the employees table.
Open snippet →The CREATE TABLE statement is used to create a new table in a database. You must define the name of the columns and their data types.
Open snippet →The ORDER BY keyword is used to sort the result-set in ascending (ASC) or descending (DESC) order. Here, products are sorted from most expensive to cheapest.
Open snippet →The WHERE clause is used to filter records. It ensures that only rows meeting a specific condition—in this case, products with a price greater than 100—are returned.
Open snippet →The COUNT() function returns the number of rows that matches a specified criterion. Using (*) counts all rows in the orders table.
Open snippet →The UPDATE statement is used to modify the existing records in a table. It is crucial to use a WHERE clause to specify which record(s) should be updated; otherwise, all records in the table will be…
Open snippet →INNER JOIN combines rows from two tables whenever there is a matching value in a common column. This allows you to retrieve data distributed across multiple tables in a single result set.
Open snippet →Aliases are used to give a table or a column in a table a temporary name. They are often used to make column names more readable or descriptive in the output.
Open snippet →The DISTINCT keyword is used in a SELECT statement to remove duplicate rows from the result set. It ensures that each returned value is unique.
Open snippet →The DELETE statement removes rows from a table. Similar to UPDATE, the WHERE clause is vital to prevent accidentally deleting all data in the table.
Open snippet →A transaction bundles multiple database modifications into a single unit of work. Using COMMIT saves all operations permanently, ensuring data stays consistent across multiple steps.
Open snippet →An index acts like a book index for a table, allowing the database engine to find specific rows much faster without scanning every record.
Open snippet →A view provides a restricted virtual table that exposes only non-sensitive columns, preventing unauthorized access to confidential fields like passwords or social security numbers.
Open snippet →Avoiding SELECT * and requesting only necessary columns reduces memory usage and network overhead, significantly improving query performance.
Open snippet →Explicitly grouping conditions with parentheses ensures correct logical operator precedence (AND vs. OR), avoiding unexpected filtering bugs.
Open snippet →A PRIMARY KEY constraint uniquely identifies each record in a database table. Primary keys must contain unique values and cannot contain NULL values.
Open snippet →Aggregate functions like COUNT and AVG compute a single result value from a set of input values across multiple table rows.
Open snippet →Column aliases assigned via the AS keyword temporarily rename output headers in query results to improve readability without altering underlying table columns.
Open snippet →Because NULL represents unknown or missing data, equality operators like = cannot compare NULL values; SQL requires using IS NULL instead.
Open snippet →When defining text columns, CHAR stores fixed-length text padding short inputs with spaces, while VARCHAR stores variable-length strings efficiently. Choosing the right data type optimizes storage…
Open snippet →Indexes improve data retrieval speed by providing a fast lookup structure on columns frequently filtered in search conditions, reducing the need for full table scans.
Open snippet →Transactions group multiple SQL operations into an all-or-nothing unit of work. COMMIT saves all changes permanently, preserving data consistency across multi-step updates.
Open snippet →Scalar functions like LOWER and UPPER transform individual column values into lowercase or uppercase, helping normalize text representation across outputs.
Open snippet →The DECIMAL numeric data type stores exact decimal numbers with specified precision and scale. It is preferred for monetary calculations to prevent rounding errors associated with floating-point ty…
Open snippet →The CONCAT scalar function combines multiple string expressions into a single text value. It simplifies formatting text output directly within SQL queries.
Open snippet →The REVOKE command removes specific database permissions from users or roles, enforcing the principle of least privilege for security compliance.
Open snippet →Creating an index on a frequently searched column builds a supporting data structure that reduces query lookup times from linear table scans to logarithmic searches.
Open snippet →The ROLLBACK command undoes all modifications performed within the current transaction, returning the database to its state before the transaction began.
Open snippet →The GRANT statement is used in SQL to provide database permissions to specific users or roles. Giving SELECT privileges allows a user to read table data without giving them permission to modify or…
Open snippet →Transactions bundle multiple SQL commands into a single unit of work. The COMMIT statement permanently saves all modifications made within the active transaction to the database.
Open snippet →