1. What does SQL stand for?
SQL stands for Structured Query Language. People use SQL to manage relational databases.
Full lesson: Sql intro
40 questions about SQL with short answers and code. Each answer links to a full lesson on this site. Free, with no signup and no ads.
SQL stands for Structured Query Language. People use SQL to manage relational databases.
Full lesson: Sql intro
The SELECT statement reads data from a table. The result is a result set.
SELECT * FROM Websites;
Full lesson: Sql select
List the column names after SELECT. The database returns only those columns.
SELECT name, country FROM Websites;
Full lesson: Sql select
The WHERE clause filters rows. It returns only rows that meet the condition.
SELECT * FROM Websites WHERE country = 'CN';
Full lesson: Sql where
Use AND when both conditions must be true. Use OR when one condition is enough.
SELECT * FROM Websites WHERE country = 'CN' AND id > 2;
Full lesson: Sql and or
LIKE searches a column for a pattern. The percent sign matches any number of characters.
SELECT * FROM Websites WHERE name LIKE 'G%';
Full lesson: Sql like
IN checks a column against a list of values. The query returns rows that match any value in the list.
SELECT * FROM Websites WHERE country IN ('CN', 'USA');
Full lesson: Sql in
Use an alias to give a table or column a short name. The alias makes the query easier to read.
SELECT w.name FROM Websites AS w;
Full lesson: Sql alias
INNER JOIN returns rows that match in both tables. Rows without a match are dropped.
SELECT * FROM Websites INNER JOIN access_log ON Websites.id = access_log.site_id;
Full lesson: Sql join inner
LEFT JOIN returns all rows from the left table. Missing values from the right table become NULL.
SELECT * FROM Websites LEFT JOIN access_log ON Websites.id = access_log.site_id;
Full lesson: Sql join left
FULL OUTER JOIN returns rows from both tables. It keeps matched rows and unmatched rows.
SELECT * FROM Websites FULL OUTER JOIN access_log ON Websites.id = access_log.site_id;
Full lesson: Sql join full
Use LIMIT in MySQL to cap the row count. SQL Server uses SELECT TOP for the same result.
SELECT * FROM Websites LIMIT 3;
Full lesson: Sql top
Use CREATE TABLE and list each column with its data type. The database then creates the table.
CREATE TABLE Sites (id INT, name VARCHAR(50));
Full lesson: Sql create table
An index helps the database find rows faster. Create indexes only on columns that you search often.
CREATE INDEX idx_name ON Websites (name);
Full lesson: Sql create index
A missing ON condition causes a cross join. The result then holds every row pair. Always add the ON clause.
SELECT * FROM Websites INNER JOIN access_log ON Websites.id = access_log.site_id;
Full lesson: Sql join inner
SQL is a language for storing and working with data in relational databases. You use it to create tables, add data, and run queries.
Full lesson: Sql intro
A basic SQL statement starts with a keyword such as SELECT. Then you name the columns and the table. End the statement with a semicolon.
SELECT * FROM Customers;
Full lesson: Sql syntax
The CREATE TABLE statement makes a new table in the database. You list each column name and its data type.
CREATE TABLE Persons (
PersonID int,
LastName varchar(255)
);
Full lesson: Sql create table
A data type tells SQL what kind of value a column holds. Examples are integer, varchar, and date. Each column needs a data type.
Full lesson: Sql datatypes general
RIGHT JOIN returns all rows from the right table. It also returns matching rows from the left table. If there is no match, the left side shows NULL.
SELECT * FROM Orders
RIGHT JOIN Customers
ON Orders.CustomerID = Customers.CustomerID;
Full lesson: Sql join right
Use the SELECT INTO statement to copy data into a new table. MySQL does not support SELECT INTO. In MySQL, use CREATE TABLE AS SELECT instead.
CREATE TABLE NewTable AS
SELECT * FROM OldTable;
Full lesson: Sql select into
Use the CREATE INDEX statement and give the index a name. Then name the table and the column. Indexes speed up searches on that column.
CREATE INDEX idx_lastname
ON Persons (LastName);
Full lesson: Sql create index
Use the DROP statement to delete an index, a table, or a database. The IF EXISTS clause avoids an error when the object is missing.
DROP TABLE IF EXISTS Shippers;
Full lesson: Sql drop
A view is a saved query that looks like a table. It shows rows and columns from one or more real tables. You can use WHERE and JOIN inside a view.
CREATE VIEW CustomerView AS
SELECT CustomerName, City
FROM Customers
WHERE City = 'London';
Full lesson: Sql view
The NOW() function returns the current date and time. You can use it in a SELECT statement or as a default value.
SELECT NOW();
Full lesson: Func now
Use the DATEDIFF() function with two dates. It returns the number of days from the second date to the first date. Only the date part counts.
SELECT DATEDIFF('2008-11-30', '2008-11-29') AS DiffDate;
Full lesson: Func datediff mysql
The DATE() function returns only the date part. It removes the time part from a date or datetime value.
SELECT DATE('2008-11-11 13:23:44');
Full lesson: Func date
A common mistake is to think RIGHT JOIN works like LEFT JOIN. RIGHT JOIN keeps all rows from the right table. If you swap the table order, the result changes.
Full lesson: Sql join right
A common mistake is to create indexes on many columns. Each index slows down updates because the index must also change. Create indexes only on columns you search often.
Full lesson: Sql create index
A common mistake is to expect a time difference in hours or minutes. MySQL DATEDIFF() returns only the number of days. It ignores the time part of each value.
Full lesson: Func datediff mysql
The UNION operator combines the results of two or more SELECT statements into one result set. Each SELECT must have the same number of columns. The column data types must be similar.
SELECT name FROM Websites
UNION
SELECT name FROM Clients;
Full lesson: Sql union
UNION removes duplicate rows from the result. UNION ALL keeps all rows, including duplicates. Use UNION ALL when you need every row.
SELECT name FROM Websites
UNION ALL
SELECT name FROM Clients;
Full lesson: Sql union
Use the ALTER TABLE statement with the ADD keyword. Name the column and its data type. The new column is added to the table.
ALTER TABLE Websites
ADD age INT;
Full lesson: Sql alter
Use the ALTER TABLE statement with the MODIFY COLUMN clause. Name the column and the new data type. SQL Server uses ALTER COLUMN instead.
ALTER TABLE Websites
MODIFY COLUMN age VARCHAR(10);
Full lesson: Sql alter
The CHECK constraint limits the values that a column can hold. You can set it on one column or on a whole table. The database rejects rows that fail the check.
CREATE TABLE Persons (
P_Id int NOT NULL,
CHECK (P_Id > 0)
);
Full lesson: Sql check
Use the CREATE VIEW statement with a name and a SELECT query. The view shows rows and columns like a real table. The data comes from one or more real tables.
CREATE VIEW usa_sites AS
SELECT name, url FROM Websites
WHERE country = 'USA';
Full lesson: Sql view
A common mistake is to select a different number of columns in each SELECT. UNION fails when the column counts do not match. Check that each SELECT returns the same number of columns.
SELECT id, name FROM Websites
UNION
SELECT id FROM Clients;
Full lesson: Sql union
A common mistake is to drop a column that other objects still need. Some database systems do not allow you to drop a column this way. Check your database rules before you run the statement.
ALTER TABLE Websites
DROP COLUMN age;
Full lesson: Sql alter
A common mistake is to forget that a view depends on real tables. If you drop a base table, the view stops working. Check the base tables before you change them.
DROP VIEW usa_sites;
Full lesson: Sql view
A common mistake is to write a check that the data cannot pass. The database then rejects every new row. Test the condition with real values first.
CREATE TABLE Persons (
Age int,
CHECK (Age > 0)
);
Full lesson: Sql check