The following is SQL CRUD operations solving problems introduced in the instructions
Find the data in (.csv format) for the first part here.
.open resturant_db.db
.mode csv
.headers on
DROP TABLE IF EXISTS resturants;
CREATE table resturants(
id INTEGER PRIMARY KEY,
resturant_name TEXT,
neighborhood TEXT,
category TEXT,
price_tier TEXT,
resturant_open TIME,
resturant_close TIME,
average_rating FLOAT,
good_for_kids INTEGER
);
.import data/resturants.csv resturants --skip 1;
DROP TABLE IF EXISTS reviews;
CREATE TABLE reviews (
id INTEGER PRIMARY KEY,
review_col TEXT
);
INSERT INTO reviews (id) SELECT id FROM resturants;
UPDATE resturants SET resturant_open = '0' || resturant_open;
SELECT resturant_name from resturants where neighborhood = "Bushwick" and price_tier = "Cheap";
2)Find all restaurants in a particular genre (pick any genre as an example) with 3 stars or more, ordered by the number of stars in descending order.
SELECT resturant_name,average_rating from resturants where category = "Mexican" and average_rating >= 3 order by average_rating DESC;
SELECT resturant_name from resturants where strftime ('%H:%M', 'now', 'localtime') BETWEEN strftime('%H:%M', resturant_open) AND strftime('%H:%M', resturant_close);
UPDATE reviews SET review_col = IFNULL(review_col || '; ', '') || 'Loved the food!' || '; ' WHERE id = 10;
delete from resturants where good_for_kids = 0;
select neighborhood, count(id) from resturants group by neighborhood;
- Find the data (in .csv format) for the posts here.
- Find the data (in .csv format) for the users here.
.open sm.db
.mode csv
.headers on
DROP TABLE IF EXISTS users;
CREATE table users(
id INTEGER PRIMARY KEY,
handle TEXT,
email TEXT,
password TEXT
);
.import data/users.csv users --skip 1;
DROP TABLE IF EXISTS posts;
CREATE table posts(
id INTEGER PRIMARY KEY,
id_sent INTEGER,
message TEXT,
viewed INTEGER,
id_recieved INTEGER,
stories TEXT,
event_datetime TIME
);
.import data/posts.csv posts --skip 1;
insert into users(
handle,
email,
password
)
values("DataWizard","[email protected]", "Pikachu");
2) Create a new Message sent by a particular User to a particular User (pick any two Users for example).
insert into posts(
id_sent,
message,
id_recieved,
event_datetime
)
values(1,"Hey, this is a super secret message", 10, strftime('%Y-%m-%d %H:%M:%S', 'now'));
insert into posts(
id_sent,
stories,
event_datetime
)
values(524, "My first story post!", strftime('%Y-%m-%d %H:%M:%S', 'now'));
select event_datetime, message, stories from posts order by event_datetime desc limit 10;
5) Show the 10 most recent visible Messages sent by a particular User to a particular User (pick any two Users for example), in order of recency.
SELECT event_datetime, message FROM posts WHERE viewed = 1 AND id_sent = 865 AND id_recieved = 627 ORDER BY event_datetime DESC LIMIT 10;
UPDATE posts set viewed = 1 WHERE message IS "" and
(ROUND((JULIANDAY('now') - JULIANDAY(event_datetime)) * 24)) > 24;
select message, stories, event_datetime FROM posts WHERE viewed = 1 ORDER BY JULIANDAY(event_datetime) DESC;
SELECT users.id, COUNT(posts.id)
FROM posts
JOIN users ON posts.id_sent = users.id
GROUP BY users.id;
9) Show the post text and email address of all posts and the User who made them within the last 24 hours.
SELECT message, stories, users.email, event_datetime
FROM posts
JOIN users ON posts.id_sent = users.id
WHERE ((ROUND((JULIANDAY('now') - JULIANDAY(event_datetime)) * 24)) < 24) LIMIT 15;
SELECT users.email FROM users LEFT JOIN posts ON users.id = posts.id_sent WHERE posts.id_sent IS NULL;