SQL in 15 days Flashcards
Udemy course.
What is a subquery in SQL?
A subquery is a query nested inside another query.
What is the syntax for a subquery in SELECT?
SELECT column1, (SELECT column2 FROM table2 WHERE condition) FROM table1;
What is the syntax for a subquery in WHERE?
SELECT column1 FROM table1 WHERE column2 = (SELECT column2 FROM table2 WHERE condition);
What is the syntax for a subquery in FROM?
SELECT subquery_table.column1 FROM (SELECT column1 FROM table1) AS subquery_table;
What is a correlated subquery?
A correlated subquery is a subquery that depends on values from the outer query.
What is the syntax for a correlated subquery?
SELECT column1 FROM table1 WHERE EXISTS (SELECT 1 FROM table2 WHERE table2.column = table1.column);
What is the UNION operator used for?
UNION combines results from two SELECT statements, removing duplicates.
What is the difference between UNION and UNION ALL?
UNION removes duplicates, while UNION ALL includes all records.
What is the syntax for an INNER JOIN?
SELECT table1.column, table2.column FROM table1 INNER JOIN table2 ON table1.id = table2.id;
What is an OUTER JOIN?
An OUTER JOIN returns matching rows plus unmatched rows from one or both tables.
What is the difference between LEFT and RIGHT JOIN?
LEFT JOIN returns all records from the left table and matching records from the right. RIGHT JOIN does the opposite.
What is the syntax for a LEFT JOIN?
SELECT column1 FROM table1 LEFT JOIN table2 ON table1.id = table2.id;
What is the syntax for a RIGHT JOIN?
SELECT column1 FROM table1 RIGHT JOIN table2 ON table1.id = table2.id;
How do you join multiple tables?
Use multiple JOIN clauses: SELECT column FROM table1 INNER JOIN table2 ON condition INNER JOIN table3 ON condition;
How do you join with multiple conditions?
Use AND: SELECT column FROM table1 JOIN table2 ON table1.id = table2.id AND table1.date = table2.date;
What does the REPLACE function do?
REPLACE substitutes a string with another: SELECT REPLACE(‘text’, ‘e’, ‘a’);
What does CAST do in SQL?
CAST converts data types: SELECT CAST(column AS datatype) FROM table;
What does COALESCE do?
COALESCE returns the first non-null value: SELECT COALESCE(NULL, ‘default’);
What is the syntax for a CASE statement?
SELECT CASE WHEN condition THEN result ELSE default END FROM table;
How does CASE work with SUM?
SELECT SUM(CASE WHEN condition THEN value ELSE 0 END) FROM table;
What are common mathematical operators in SQL?
+, -, *, /, % are commonly used for arithmetic calculations.
What does the LENGTH function do?
LENGTH returns the number of characters in a string.
How do you convert text to lowercase?
Use LOWER: SELECT LOWER(‘TEXT’);
How do you convert text to uppercase?
Use UPPER: SELECT UPPER(‘text’);