Hi Coders!🐱💻
Richa here again, coming with a new topic to learn - SQL! Whether you're just starting out or prepping for an interview, this SQL cheat sheet will be your best friend. It doesn't delve into the nitty-gritty details, but it covers essential SQL commands you need to know. For more in-depth learning and practice, check out these great resources:
Now, let's dive into the basics!
Basic Queries & Operators
Selecting Data
SELECT column1, column2 FROM table;
-- Query data in columns column1, column2 from a table.
SELECT * FROM table;
-- Query all rows and columns from a table.
SELECT column1, column2 FROM table WHERE condition;
-- Query data and filter rows using a boolean condition: =, <, <=, >, >=, <>.
SELECT column1, column2 FROM table1 WHERE column1 [NOT] LIKE pattern;
-- Query rows using pattern matching. Use with % or _.
SELECT column1, column2 FROM table WHERE column1 [NOT] IN (value1, value2, ...);
-- Filters rows with values equal to those in the value_list.
SELECT column1, column2 FROM table WHERE column1 BETWEEN limit1 AND limit2;
-- Filters rows with values between the two limits.
SELECT column1, column2 FROM table WHERE column1 IS [NOT] NULL;
-- Filters NULL values.
SELECT DISTINCT column1 FROM table WHERE condition;
-- Returns distinct rows from a table.
SELECT column1, column2 FROM table WHERE rownum < n;
-- Returns the first n rows.
JOINs
SELECT column1, column2 FROM table1 INNER JOIN table2 ON condition;
-- Inner join table1 and table2.
SELECT column1, column2 FROM table1 LEFT JOIN table2 ON condition;
-- Left join table1 and table2.
SELECT column1, column2 FROM table1 RIGHT JOIN table2 ON condition;
-- Right join table1 and table2.
SELECT column1, column2 FROM table1 FULL OUTER JOIN table2 ON condition;
-- Full outer join table1 and table2.
SELECT column1, column2 FROM table1 CROSS JOIN table2;
-- Cross join table1 and table2 (Cartesian product).
SELECT column1, column2 FROM table1 A INNER JOIN table1 B ON condition;
-- Self join table1 to itself using INNER JOIN.
Order, Group, Aggregate
SELECT column1, column2 FROM table ORDER BY column1 [ASC|DESC];
-- Sorts the results in ascending or descending order.
SELECT column1, aggregate_function(column2) FROM table GROUP BY column1;
-- Groups rows using an aggregate function.
SELECT column1, aggregate_function(column2) FROM table GROUP BY column1 HAVING condition;
-- Filter groups using HAVING operator.
Aggregate Functions:
AVG(column)
- Returns the average of a list.COUNT(column)
- Returns the number of elements.SUM(column)
- Returns the total.MAX(column)
- Returns the maximum value.MIN(column)
- Returns the minimum value.
Data Definition Language (DDL)
CREATE TABLE table_name(
id NUMBER PRIMARY KEY,
column_name1 VARCHAR2 NOT NULL,
column_name2 DATE
);
-- Creates a new table with three columns.
DROP TABLE table_name;
-- Deletes table from the database.
ALTER TABLE table_name ADD column_name datatype;
-- Adds a new column to the table.
ALTER TABLE table_name RENAME column_name1 TO column_name2;
-- Renames column column_name1 to column_name2.
ALTER TABLE table_name DROP COLUMN column_name;
-- Removes column from the table.
ALTER TABLE old_table_name RENAME TO new_table_name;
-- Renames a table.
TRUNCATE TABLE table_name;
-- Removes all data in a table.
Data Manipulation Language (DML)
INSERT INTO table_name (column_list) VALUES (value_list);
-- Inserts one record into a table.
INSERT INTO table1 (column_list) SELECT column_list FROM table2;
-- Inserts rows from table2 into table1. Columns types must match!
UPDATE table SET column1 = new_value, column2 = new_value;
-- Updates values in columns for all rows.
UPDATE table SET column1 = new_value, column2 = new_value WHERE condition;
-- Updates values in columns that match the condition.
DELETE FROM table_name;
-- Deletes all data in a table.
DELETE FROM table_name WHERE condition;
-- Deletes rows that match the condition.
Constraints, Views, Triggers
Constraints
CREATE TABLE table1 (
col1 NUMBER PRIMARY KEY,
col2 NUMBER NOT NULL,
FOREIGN KEY (col2) REFERENCES table2(col2),
col3 NUMBER,
UNIQUE (col3),
CHECK (col3 > 0 AND col3 >= col2)
);
-- Defines various constraints on table columns.
Views
CREATE [TEMPORARY] VIEW view_name (col1, col2) AS
SELECT col1, col2 FROM table;
-- Creates a new view consisting of two columns from a table.
DROP VIEW view_name;
-- Deletes the view.
Triggers
CREATE [OR ALTER] TRIGGER trigger_name
BEFORE [OR AFTER] EVENT
ON table_name FOR EACH ROW [OR STATEMENT]
BEGIN
-- Trigger logic here
END;
-- Creates or modifies a trigger.
-- EVENT values: INSERT, UPDATE, DELETE
DROP TRIGGER trigger_name;
-- Deletes a trigger.
🎯 Wrap Up!!
Remember, this is just a cheat sheet to get you started. For detailed explanations and more examples, make sure to check the resources mentioned above. Happy coding!
If you have any questions or suggestions, please leave a comment below.