GithubHelp home page GithubHelp logo

wilhelmvonarndt / sql-crud Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 207 KB

SQL backend coding for a hypothetical app.

License: GNU General Public License v3.0

Python 100.00%
app-backend sql sqliite

sql-crud's Introduction

SQL CRUD

The following is SQL CRUD operations solving problems introduced in the instructions

PART 1 - RESTURANT FINDER

Find the data in (.csv format) for the first part here.

importing data

.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;

1) Find all cheap restaurants in a particular neighborhood (pick any neighborhood as an example).

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;

3) Find all restaurants that are open now (see hint below).

SELECT resturant_name from resturants where strftime ('%H:%M', 'now', 'localtime') BETWEEN strftime('%H:%M', resturant_open) AND strftime('%H:%M', resturant_close);

4) Leave a review for a restaurant (pick any restaurant as an example).

UPDATE reviews SET review_col = IFNULL(review_col || '; ', '') || 'Loved the food!' || '; ' WHERE id = 10;

5) Delete all restaurants that are not good for kids.

delete from resturants where good_for_kids = 0;

6) Find the number of restaurants in each NYC neighborhood.

select neighborhood, count(id) from resturants group by neighborhood;

PART 2 - SOCIAL MEDIA APP

  • Find the data (in .csv format) for the posts here.
  • Find the data (in .csv format) for the users here.

importing data

.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;

1) Register a new User.

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'));

3) Create a new Story by a particular User (pick any User for example).

insert into posts(
  id_sent,
  stories,
  event_datetime
)

values(524, "My first story post!", strftime('%Y-%m-%d %H:%M:%S', 'now'));

4) Show the 10 most recent visible Messages and Stories, in order of recency.

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;

6) Make all Stories that are more than 24 hours old invisible.

UPDATE posts set viewed = 1 WHERE message IS "" and
(ROUND((JULIANDAY('now') - JULIANDAY(event_datetime)) * 24)) > 24;

 

7) Show all invisible Messages and Stories, in order of recency.

select message, stories, event_datetime FROM posts WHERE viewed = 1 ORDER BY JULIANDAY(event_datetime) DESC;

8) Show the number of posts by each User.

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;

10) Show the email addresses of all Users who have not posted anything yet.

SELECT users.email FROM users LEFT JOIN posts ON  users.id = posts.id_sent WHERE posts.id_sent IS NULL;

sql-crud's People

Contributors

wilhelmvonarndt avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.