https://github.com/bluefrog130/sql-basics
https://github.com/bluefrog130/sql-basics
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/bluefrog130/sql-basics
- Owner: BlueFrog130
- Created: 2021-09-05T02:18:39.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-09-08T03:57:38.000Z (over 4 years ago)
- Last Synced: 2025-10-09T17:03:16.893Z (8 months ago)
- Size: 1.8 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SQL Basics
Showing basic SQL commands and how to query & filter data.
[Download the people data](https://github.com/BlueFrog130/SQL-Basics/raw/main/data.csv)
Creating the table
```SQL
USE tutorial;
CREATE TABLE People (
id INT NOT NULL AUTO_INCREMENT,
`first` VARCHAR(100) NOT NULL,
middle CHAR(1) NOT NULL,
`last` VARCHAR(100) NOT NULL,
sex VARCHAR(6) NOT NULL,
state CHAR(2) NOT NULL,
birthday DATETIME NOT NULL,
PRIMARY KEY ( id )
);
```
Selecting everything from the people table
```SQL
USE tutorial;
SELECT * FROM people;
```
Inserting a single value
```SQL
USE tutorial;
INSERT INTO people (`first`, middle, `last`, sex, state, birthday) VALUES
('Adam', 'l', 'Grady', 'male', 'SD', '1998-08-14');
```
Inserting from a csv
```SQL
USE tutorial;
LOAD DATA INFILE 'C:/Users//Downloads/data.csv'
INTO TABLE People
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(@`first`, @middle, @`last`, @sex, @state, @birthday)
SET `first`=@`first`, middle=@middle, `last`=@`last`, sex=@sex, state=@state, birthday=@birthday;
```
Altering the table
```SQL
USE tutorial;
ALTER TABLE People ADD COLUMN FullName TEXT GENERATED ALWAYS AS (CONCAT(`first`, ' ', `last`));
```
Some more `SELECT` statements
```SQL
USE tutorial;
# Counts everything from the people table
SELECT COUNT(*) FROM People;
# Gets people's id, first name, and state where their state is from SD. Then limits to top 100 results.
SELECT id, `first`, state FROM people WHERE state = 'SD' LIMIT 100;
# Selects a persons full name and age
SELECT FullName, TIMESTAMPDIFF(YEAR, birthday, CURDATE()) AS Age FROM People LIMIT 100;
# Groups people by sex, then computes the average age and counts them
SELECT
sex AS Sex,
AVG(TIMESTAMPDIFF(YEAR, birthday, CURDATE())) AS AvgAge,
COUNT(*) AS Amount
FROM People GROUP BY sex;
```