Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

We in this! #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions SQL.BuildAndDestroy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Build and Destroy: Introductory SQL Queries

Given the scenarios, add the the directed queries to the "answers" directory's files.

## Creating databases

Write the correct SQL statement to create a new database called **myNewDB**.

*(Add your query to the file exercise1.sql)*

## Deleting databases

Write the correct SQL statement to delete a database named **myNewDB**.

*(Add your query to the file exercise2.sql)*

## Creating tables

Write the correct SQL statement to create a new table called **Users**, with an int field called **UserID**, and the following varchar fields of size 255: **LastName, FirstName, Address, City**

*(Add your query to the file exercise3.sql)*

## Deleting tables

Write the correct SQL statement to delete a table called **Users**.

*(Add your query to the file exercise4.sql)*


Use the **TRUNCATE** statement to delete all data inside the **Users** table.

*(Add your query to the file exercise5.sql)*

## Altering tables

Add a column of type **DATE** called **Birthday** to the **Users** table.

*(Add your query to the file exercise6.sql)*

Delete the column **Birthday** from the **Users** table.

*(Add your query to the file exercise7.sql)*


## Inserting records

Insert a new record in the **Students** table.

**Schema:**

```
StudentName,
Address,
City,
PostalCode,
Country
```

**Record's info to enter:**

```
Jane Doe
57 Union St
Glasgow, Scotland
G13RB
```

*(Add your query to the file exercise8.sql)*

## Selecting Records

### Where

Use the **NOT** keyword to select all records in the **Students** table where **City** is NOT "Philadelphia".

*(Add your query to the file exercise9.sql)*

Select all records in the **Students** table where the **City** column has the value 'Philadelphia' or 'Trenton'.

*(Add your query to the file exercise10.sql)*

### Order By
Select all records from the **Students** table, sort the result alphabetically by the column **City**.

*(Add your query to the file exercise11.sql)*

Select all records from the **Students** table, sort the result reversed alphabetically by the column **City**.

*(Add your query to the file exercise12.sql)*

Select all records from the **Students** table, sort the result alphabetically, first by the column **Country**, then by the column **City**.

*(Add your query to the file exercise13.sql)*

### Null values
Select all records from the **Students** where the **PostalCode** column is empty.

*(Add your query to the file exercise14.sql)*

Select all records from the **Students** where the **PostalCode** column is **NOT** empty.

*(Add your query to the file exercise15.sql)*


## Updating records
Update the **City** column of all records in the **Students** table and set it to "Edinburgh".

*(Add your query to the file exercise16.sql)*

Set the value of the **City** columns to "Edinburgh", but only the ones where the **Country** column has the value "Scotland".

*(Add your query to the file exercise17.sql)*

Update the **City** value and the **Country** value to "Edinburgh", "Scotland" in the **Students** table, for the Student whose ID is 35.

*(Add your query to the file exercise18.sql)*

## Deleting Records
Delete all the records from the **Students** table where the **Country** value is "Scotland".

*(Add your query to the file exercise19.sql)*

Delete all the records from the **Students** table.

*(Add your query to the file exercise20.sql)*
1 change: 1 addition & 0 deletions SQL.BuildAndDestroy/answers/exercise1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE DATABASE myNewDB;
1 change: 1 addition & 0 deletions SQL.BuildAndDestroy/answers/exercise10.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM Students WHERE City IN ('Trenton', 'Philadelphia');
1 change: 1 addition & 0 deletions SQL.BuildAndDestroy/answers/exercise11.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM Students ORDER BY City;
1 change: 1 addition & 0 deletions SQL.BuildAndDestroy/answers/exercise12.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM Students ORDER BY City DESC;
1 change: 1 addition & 0 deletions SQL.BuildAndDestroy/answers/exercise13.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM Students ORDER BY Country , City;
14 changes: 14 additions & 0 deletions SQL.BuildAndDestroy/answers/exercise14.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
INSERT INTO Students(
StudentName,
Address,
City,
Country,
PostalCode)
VALUES(
'Steve-o',
'57 Union St',
'Glasgow',
'Scotland',
NULL);

SELECT * FROM Students WHERE PostalCode IS NULL;
1 change: 1 addition & 0 deletions SQL.BuildAndDestroy/answers/exercise15.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM Students WHERE PostalCode IS NOT NULL;
2 changes: 2 additions & 0 deletions SQL.BuildAndDestroy/answers/exercise16.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
UPDATE Students SET City = 'Edinburgh';
SET SQL_SAFE_UPDATES = 0;
1 change: 1 addition & 0 deletions SQL.BuildAndDestroy/answers/exercise17.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE Students SET City = 'Edinburgh' WHERE Country = 'Scotland';
1 change: 1 addition & 0 deletions SQL.BuildAndDestroy/answers/exercise18.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE Students SET City = 'Edinburgh' AND Country = 'Scotland' WHERE ID =35;
1 change: 1 addition & 0 deletions SQL.BuildAndDestroy/answers/exercise19.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE FROM Students WHERE Country = 'Scotland';
1 change: 1 addition & 0 deletions SQL.BuildAndDestroy/answers/exercise2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP DATABASE myNewDB;
1 change: 1 addition & 0 deletions SQL.BuildAndDestroy/answers/exercise20.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Delete FROM Students;
6 changes: 6 additions & 0 deletions SQL.BuildAndDestroy/answers/exercise3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
USE myNewDB;
CREATE TABLE Users(
UserID int, LastName VARCHAR(255),
FirstName VARCHAR(255), Address VARCHAR(255),
City VARCHAR(255));
DESCRIBE Users;
1 change: 1 addition & 0 deletions SQL.BuildAndDestroy/answers/exercise4.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE Users;
1 change: 1 addition & 0 deletions SQL.BuildAndDestroy/answers/exercise5.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TRUNCATE TABLE Users;
3 changes: 3 additions & 0 deletions SQL.BuildAndDestroy/answers/exercise6.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE Users
ADD Birthday DATE;
DESCRIBE Users;
3 changes: 3 additions & 0 deletions SQL.BuildAndDestroy/answers/exercise7.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE Users
DROP Birthday;
DESCRIBE Users;
22 changes: 22 additions & 0 deletions SQL.BuildAndDestroy/answers/exercise8.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CREATE TABLE Students(
StudentName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255),
PostalCode VARCHAR(255),
Country VARCHAR(255));


INSERT INTO Students(
StudentName,
Address,
City,
Country,
PostalCode)
VALUES(
'Jane Doe',
'57 Union St',
'Glasgow',
'Scotland',
'G13RB');

SELECT * FROM Students;
1 change: 1 addition & 0 deletions SQL.BuildAndDestroy/answers/exercise9.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM Students WHERE City NOT IN ('Philadelphia');