learnxinyminutes-docs/sql.md

159 lines
6.9 KiB
Markdown
Raw Normal View History

2018-10-29 22:16:47 +00:00
---
name: SQL
2018-10-29 22:16:47 +00:00
filename: learnsql.sql
contributors:
- ["Bob DuCharme", "http://bobdc.com/"]
2024-04-19 08:52:08 +00:00
- ["Th3G33k", "https://github.com/Th3G33k"]
2024-04-27 10:10:02 +00:00
2018-10-29 22:16:47 +00:00
---
2020-09-22 13:13:57 +00:00
Structured Query Language (SQL) is an [ISO/IEC 9075](https://www.iso.org/standard/63555.html) standard language for creating and working with databases stored in a set of tables. Implementations usually add their own extensions to the language; [Comparison of different SQL implementations](http://troels.arvin.dk/db/rdbms/) is a good reference on product differences.
2018-10-29 22:16:47 +00:00
Implementations typically provide a command line prompt where you can enter the commands shown here interactively, and they also offer a way to execute a series of these commands stored in a script file. (Showing that youre done with the interactive prompt is a good example of something that isnt standardized--most SQL implementations support the keywords QUIT, EXIT, or both.)
2024-04-19 05:58:53 +00:00
Several of these sample commands assume that the [MySQL employee sample database](https://dev.mysql.com/doc/employee/en/) available on [GitHub](https://github.com/datacharmer/test_db) has already been loaded. The GitHub files are scripts of commands, similar to the relevant commands below, that create and populate tables of data about a fictional companys employees. The syntax for running these scripts will depend on the SQL implementation you are using. A utility that you run from the operating system prompt is typical.
2018-10-29 22:16:47 +00:00
```sql
2018-11-04 17:26:31 +00:00
-- Comments start with two hyphens. End each command with a semicolon.
2018-10-29 22:16:47 +00:00
2024-04-19 08:52:08 +00:00
/*
Multi-line comments
*/
2018-11-04 17:26:31 +00:00
-- SQL is not case-sensitive about keywords. The sample commands here
2019-10-12 14:55:05 +00:00
-- follow the convention of spelling them in upper-case because it makes
2018-11-04 17:26:31 +00:00
-- it easier to distinguish them from database, table, and column names.
2018-10-29 22:16:47 +00:00
2018-11-04 17:26:31 +00:00
-- Create and delete a database. Database and table names are case-sensitive.
2018-10-29 22:16:47 +00:00
CREATE DATABASE someDatabase;
DROP DATABASE someDatabase;
2018-11-04 17:26:31 +00:00
-- List available databases.
2018-10-29 22:16:47 +00:00
SHOW DATABASES;
2019-10-12 14:55:05 +00:00
-- Use a particular existing database.
2018-11-04 17:37:45 +00:00
USE employees;
2018-10-29 22:16:47 +00:00
2018-11-04 17:26:31 +00:00
-- Select all rows and columns from the current database's departments table.
2019-10-12 14:55:05 +00:00
-- Default activity is for the interpreter to scroll the results on your screen.
2018-10-29 22:16:47 +00:00
SELECT * FROM departments;
2019-10-12 14:55:05 +00:00
-- Retrieve all rows from the departments table,
-- but only the dept_no and dept_name columns.
2018-11-04 17:26:31 +00:00
-- Splitting up commands across lines is OK.
2018-10-29 22:16:47 +00:00
SELECT dept_no,
dept_name FROM departments;
2019-10-12 14:55:05 +00:00
-- Retrieve all departments columns, but just 5 rows.
2018-10-29 22:16:47 +00:00
SELECT * FROM departments LIMIT 5;
2018-11-04 17:26:31 +00:00
-- Retrieve dept_name column values from the departments
2019-10-12 14:55:05 +00:00
-- table where the dept_name value has the substring 'en'.
2018-11-04 17:37:45 +00:00
SELECT dept_name FROM departments WHERE dept_name LIKE '%en%';
2018-10-29 22:16:47 +00:00
2018-11-04 17:26:31 +00:00
-- Retrieve all columns from the departments table where the dept_name
2019-10-12 14:55:05 +00:00
-- column starts with an 'S' and has exactly 4 characters after it.
2018-11-04 17:37:45 +00:00
SELECT * FROM departments WHERE dept_name LIKE 'S____';
2018-10-29 22:16:47 +00:00
2018-11-04 17:26:31 +00:00
-- Select title values from the titles table but don't show duplicates.
2018-10-29 22:16:47 +00:00
SELECT DISTINCT title FROM titles;
2019-10-12 14:55:05 +00:00
-- Same as above, but sorted (case-sensitive) by the title values.
2024-04-19 08:52:08 +00:00
-- The order can be specified by adding ASC (ascending) or DESC (descending).
-- If omitted, it will sort in ascending order by default.
SELECT DISTINCT title FROM titles ORDER BY title ASC;
-- Use the comparison operators (=, >, <, >=, <=, <>) and
-- the conditional keywords (AND, OR) to refine your queries.
2024-04-27 10:10:02 +00:00
SELECT * FROM departments WHERE dept_no = 'd001' OR dept_no = 'd002';
2024-04-19 08:52:08 +00:00
-- Same as above.
2024-04-27 10:10:02 +00:00
SELECT * FROM departments WHERE dept_no IN ('d001', 'd002');
2024-04-19 08:52:08 +00:00
-- Opposite of the above.
2024-04-27 10:10:02 +00:00
SELECT * FROM departments WHERE dept_no NOT IN ('d001', 'd002');
2024-04-19 08:52:08 +00:00
-- Select in a given range.
2024-04-27 10:10:02 +00:00
SELECT * from departments WHERE dept_no BETWEEN 'd001' AND 'd002';
2018-10-29 22:16:47 +00:00
2018-11-04 17:26:31 +00:00
-- Show the number of rows in the departments table.
2018-10-29 22:16:47 +00:00
SELECT COUNT(*) FROM departments;
2018-11-04 17:26:31 +00:00
-- Show the number of rows in the departments table that
2019-10-12 14:55:05 +00:00
-- have 'en' as a substring of the dept_name value.
2018-11-04 17:37:45 +00:00
SELECT COUNT(*) FROM departments WHERE dept_name LIKE '%en%';
2018-10-29 22:16:47 +00:00
2024-04-19 08:52:08 +00:00
-- Aggregate functions can be used, with GROUP BY, to compute a value
-- from a set of values. Most commonly used functions are:
-- MIN(), MAX(), COUNT(), SUM(), AVG().
-- Use HAVING to filter rows by aggregated values.
-- Retrieve the total number of employees, by department number,
-- with the condition of having more than 100 employees.
SELECT dept_no, COUNT(dept_no) FROM dept_emp GROUP BY dept_no
2024-04-27 10:10:02 +00:00
HAVING COUNT(dept_no) > 100;
2024-04-19 08:52:08 +00:00
-- Aliases, using the optional keyword AS, can be used for column/table names.
SELECT COUNT(A.*) AS total_employees, COUNT(B.*) total_departments
2024-04-27 10:10:02 +00:00
FROM employees AS A, departments B;
2024-04-19 08:52:08 +00:00
-- Common date format is "yyyy-mm-dd".
-- However, it can vary according to the implementation, the operating system, and the session's locale.
2024-04-27 10:10:02 +00:00
SELECT * FROM dept_manager WHERE from_date >= '1990-01-01';
2024-04-19 08:52:08 +00:00
2019-10-12 14:55:05 +00:00
-- A JOIN of information from multiple tables: the titles table shows
-- who had what job titles, by their employee numbers, from what
2018-11-04 17:26:31 +00:00
-- date to what date. Retrieve this information, but instead of the
2019-10-12 14:55:05 +00:00
-- employee number, use the employee number as a cross-reference to
2018-11-04 17:26:31 +00:00
-- the employees table to get each employee's first and last name
-- instead. (And only get 10 rows.)
2018-10-29 22:16:47 +00:00
SELECT employees.first_name, employees.last_name,
titles.title, titles.from_date, titles.to_date
FROM titles INNER JOIN employees ON
employees.emp_no = titles.emp_no LIMIT 10;
2024-04-19 08:52:08 +00:00
-- Combine the result of multiple SELECT.
-- UNION selects distinct rows, UNION ALL selects all rows.
SELECT * FROM departments WHERE dept_no = 'd001'
UNION
2024-04-27 10:10:02 +00:00
SELECT * FROM departments WHERE dept_no = 'd002';
2024-04-19 08:52:08 +00:00
-- SQL syntax order is:
-- SELECT _ FROM _ JOIN _ ON _ WHERE _ GROUP BY _ HAVING _ ORDER BY _ UNION
2018-11-04 17:26:31 +00:00
-- List all the tables in all the databases. Implementations typically provide
-- their own shortcut command to do this with the database currently in use.
2018-10-29 22:16:47 +00:00
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE';
2018-11-04 17:26:31 +00:00
-- Create a table called tablename1, with the two columns shown, for
-- the database currently in use. Lots of other options are available
-- for how you specify the columns, such as their datatypes.
2018-11-04 17:52:15 +00:00
CREATE TABLE tablename1 (fname VARCHAR(20), lname VARCHAR(20));
2018-10-29 22:16:47 +00:00
2019-10-12 14:55:05 +00:00
-- Insert a row of data into the table tablename1. This assumes that the
-- table has been defined to accept these values as appropriate for it.
2018-10-29 22:16:47 +00:00
INSERT INTO tablename1 VALUES('Richard','Mutt');
2018-11-04 17:37:45 +00:00
-- In tablename1, change the fname value to 'John'
2019-10-12 14:55:05 +00:00
-- for all rows that have an lname value of 'Mutt'.
2018-11-04 17:37:45 +00:00
UPDATE tablename1 SET fname='John' WHERE lname='Mutt';
2018-10-29 22:16:47 +00:00
2018-11-04 17:26:31 +00:00
-- Delete rows from the tablename1 table
2018-11-04 17:37:45 +00:00
-- where the lname value begins with 'M'.
DELETE FROM tablename1 WHERE lname LIKE 'M%';
2018-10-29 22:16:47 +00:00
2018-11-04 17:26:31 +00:00
-- Delete all rows from the tablename1 table, leaving the empty table.
2018-10-29 22:16:47 +00:00
DELETE FROM tablename1;
2019-10-12 14:55:05 +00:00
-- Remove the entire tablename1 table.
2018-10-29 22:16:47 +00:00
DROP TABLE tablename1;
```
2019-10-12 14:55:05 +00:00
## Further Reading
* [Codecademy - SQL](https://www.codecademy.com/learn/learn-sql) A good introduction to SQL in a "learn by doing it" format.
* [Database System Concepts](https://www.db-book.com) book's Chapter 3 - Introduction to SQL has an in depth explanation of SQL concepts.