Explore tens of thousands of sets crafted by our community.
Basic SQL Queries
26
Flashcards
0/26
CREATE TABLE
Used to create a new table. Syntax example: CREATE TABLE table_name (column1 datatype, column2 datatype, ...);
LEFT JOIN
Returns all records from the left table, and the matched records from the right table. Syntax example: SELECT column1, column2 FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;
UNION
Used to combine the result sets of two or more SELECT statements. Syntax example: SELECT column1 FROM table1 UNION SELECT column1 FROM table2;
CASE
Provides conditional logic within SQL. Syntax example: SELECT column1, CASE WHEN condition THEN result1 ELSE result2 END FROM table_name;
SELECT DISTINCT
Used to return only distinct (different) values. Syntax example: SELECT DISTINCT column1 FROM table_name;
WHERE
Used to filter records. Syntax example: SELECT column1 FROM table_name WHERE condition;
INSERT INTO
Used to insert new records. Syntax example: INSERT INTO table_name (column1, column2) VALUES (value1, value2);
CREATE DATABASE
Used to create a new database. Syntax example: CREATE DATABASE dbname;
INDEX
Used to create an index on table columns. Syntax example: CREATE INDEX idx_column ON table_name (column_name);
TRUNCATE TABLE
Used to delete all rows from a table without logging individual row deletions. Syntax example: TRUNCATE TABLE table_name;
DROP TABLE
Used to delete a table. Syntax example: DROP TABLE table_name;
ROLLBACK
Used to undo transactions that haven't been saved to the database. Syntax example: ROLLBACK;
ALTER TABLE
Used to add, delete, or modify columns in an existing table. Syntax example: ALTER TABLE table_name ADD column_name datatype;
SELECT
Used to select data from a database. Syntax example: SELECT column1, column2 FROM table_name;
INNER JOIN
Used to combine rows from two or more tables, based on a related column. Syntax example: SELECT column1, column2 FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
UPDATE
Used to modify existing records. Syntax example: UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
FULL OUTER JOIN
Returns all records when there is a match in either left or right table. Syntax example: SELECT column1, column2 FROM table1 FULL OUTER JOIN table2 ON table1.column_name = table2.column_name;
INTERSECT
Returns distinct rows that are available in both SELECT statement results. Syntax example: SELECT column1 FROM table1 INTERSECT SELECT column1 FROM table2;
DELETE
Used to delete records. Syntax example: DELETE FROM table_name WHERE condition;
GROUP BY
Used with aggregate functions to group the result set by one or more columns. Syntax example: SELECT COUNT(column1), column2 FROM table_name GROUP BY column2;
ORDER BY
Used to sort the result set. Syntax example: SELECT column1, column2 FROM table_name ORDER BY column1 ASC, column2 DESC;
HAVING
Used to filter records that work with aggregate functions. Syntax example: SELECT COUNT(column1), column2 FROM table_name GROUP BY column2 HAVING COUNT(column1) > 10;
EXCEPT
Returns distinct rows from the first SELECT statement that are not in the second SELECT statement. Syntax example: SELECT column1 FROM table1 EXCEPT SELECT column1 FROM table2;
USE
Specifies the database to be used. Syntax example: USE dbname;
RIGHT JOIN
Returns all records from the right table, and the matched records from the left table. Syntax example: SELECT column1, column2 FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;
COMMIT
Used to save all transactions made within the current transaction block. Syntax example: COMMIT;
© Hypatia.Tech. 2024 All rights reserved.