capypad
0 day streak
Code library

Code worth reading.

Hand-picked snippets across TypeScript, Python, Rust, Go, and more — each one comes with a line-by-line breakdown so you can read it like the people who wrote it.

447 snippets · 10 languages
SQL · BeginnerSnippet

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.

snippet.sql
sql
1
2
SELECT first_name, last_name
FROM employees;
syntaxqueries
SQL · BeginnerSnippet

Creating a Table

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.

snippet.sql
sql
1
2
3
4
CREATE TABLE users (
id INTEGER,
username VARCHAR(50)
);
syntaxdatatypes
SQL · BeginnerSnippet

Sorting Results

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.

snippet.sql
sql
1
2
3
SELECT product_name, price
FROM products
ORDER BY price DESC;
queriessyntax
SQL · BeginnerSnippet

Filtering with WHERE

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.

snippet.sql
sql
1
2
3
SELECT *
FROM products
WHERE price > 100;
syntaxqueries
SQL · BeginnerSnippet

Aggregate Functions: COUNT

The COUNT() function returns the number of rows that matches a specified criterion. Using (*) counts all rows in the orders table.

snippet.sql
sql
1
2
SELECT COUNT(*)
FROM orders;
functionsqueries
SQL · BeginnerSnippet

Updating Existing Data

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 changed.

snippet.sql
sql
1
2
3
UPDATE employees
SET salary = 5500
WHERE employee_id = 101;
syntaxqueries
SQL · BeginnerSnippet

Joining Tables with INNER JOIN

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.

snippet.sql
sql
1
2
3
SELECT orders.order_id, customers.name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.id;
syntaxqueries
SQL · BeginnerSnippet

Using Column Aliases

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.

snippet.sql
sql
1
2
SELECT product_name AS item, unit_price AS price
FROM inventory;
syntaxbestpractices