RISHABH LALA
  • Home
  • BLOG
  • About Me
  • INTERESTS
    • AI/Machine Learning >
      • Machine Learning
      • Machine Learning_Complete
      • ML|Text2Speech
    • Statistics 4 Business >
      • Survival | Multilevel | GLM
      • Statistics| Max Likelyhood and OLS
      • Probability Distribution Functions
      • Log and Exponential Transformation
      • Heteroskendasticity and Robust Methods
      • Statistics| Basics II
      • Statistics| Basics I
    • Cloud Architecture >
      • AWS Intro >
        • AWS | Hands On 1
      • Cloud Computing
      • Cloud Architecting
      • BIG DATA >
        • MapReduce
        • SPARK
    • Web Development >
      • WEB APP DEV
      • Java Script
      • Java
      • Network Security
    • BIG DATA FOR BUSINESS >
      • SQL
    • Business Analytics >
      • Lift Curves
      • Market Basket Analysis
    • Valuation | Risk Free Rate >
      • Valuation | Example DCW_Part I
      • Valuation | Example DCW_Part II
      • Valuation | The Idea
      • Valuation | Financial Statements
      • Valuation | DCF & Risk Free Rate
      • Valuation|Equity Risk Premium
      • Valuation | Relative Valuation
      • Valuation | Terminal Value
      • Investing
    • Visualizations
    • Skill Set
    • Academics
  • My Apps
  • Articles
    • Engineering Success
    • Why Hire Me
    • My Poems

SQL Queries ( I made for one of the assignments)


### SQL Queries to Address the Questions

-- Q1: Load customer profile for a given user_id

```
SELECT * FROM user_profiles
WHERE user_id = 111222333;
```

-- Q2: Find nearby cars based on location (requires a function or stored procedure to calculate distance)

```
DELIMITER $$

CREATE PROCEDURE nearbycars(
    IN Range DECIMAL(10,2),
    IN pax_lat DECIMAL(10,2),
    IN pax_long DECIMAL(10,2)
)
BEGIN
    SELECT car_id, driver_id,
           SQRT(
               POW(driver_location_lat - pax_lat, 2) +
               POW(driver_location_long - pax_long, 2)
           ) AS distance_to_pax
    FROM nearby_cars
    WHERE driver_location_lat BETWEEN pax_live_lat - Range AND pax_live_lat + Range
      AND driver_location_long BETWEEN pax_live_long - Range AND pax_live_long + Range
      AND is_available = TRUE
    ORDER BY distance_to_pax ASC;
END$$

DELIMITER ; -- pax_live_lat and pax_live_long come from the APP; assuming Earth is flat for short distances (for using Euclidean Geometry)

```

-- Q3: Get Trip Details for a given trip_id

```
SELECT trip_id, driver_id, car_type, estimated_drop_off_time, fare, driver_first_name, driver_last_name, driver_overall_rating
FROM trip_details
WHERE trip_id = 12233344455555;-- trip_id would come from App
```

-- Q4: Get driver details for a given driver_id

```
SELECT first_name, last_name, overall_rating, miles_driven
FROM driver_information
WHERE driver_id = 1212; -- driver_id would come from App
```

-- Q5: Get Passenger details for a given user_id

```
SELECT first_name, last_name, overall_rating, mobile_number, email
FROM passenger_details
WHERE user_id = 1111; -- driver_id would come from App
```

-- Q6: Compute the surge fare based on demand level

```
SELECT multiplier FROM surge_pricing
WHERE demand_level = 'high'; -- demand_level would come from App
```

-- Q7: Calculate Total Earnings for a driver within a date range

```
SELECT SUM(fare + tips) AS total_earnings
FROM driver_earnings
WHERE driver_id = 1122 AND start_datetime BETWEEN 0000 AND 2400; -- driver_id  would come from App
```

-- Q8: Identify the most common pickup locations for drivers

```
SELECT pickup_address, COUNT(*) AS popular_pickup_locations
FROM pickup_locations_analytics
WHERE pickup_lat BETWEEN driver_live_location_lat - Range AND driver_live_location_lat + Range
      AND pickup_long BETWEEN driver_live_location_long - Range AND driver_live_location_long + Range
GROUP BY pickup_address
ORDER BY popular_pickup_locations DESC
LIMIT 3;-- driver_live_location_lat and driver_live_location_long would come from App which would be compared with pickup_lat and pickup_long from the database
```

-- Q9: Identify the most common destination locations for passengers

```
SELECT destination_address, COUNT(*) AS popular_destinations
FROM destination_locations_analytics
WHERE destination_lat BETWEEN pax_live_location_lat - Range AND pax_live_location_lat + Range
      AND destination_long BETWEEN pax_live_location_long - Range AND pax_live_location_long + Range
GROUP BY destination_address
ORDER BY popular_destinations DESC
LIMIT 3; -- pax_live_location_lat and pax_live_location_long would come from App which would be compared with destination_lat and destination_long from the database
```

-- Q10: Calculate Fare for a trip (Assuming necessary data like tolls, tax rate, etc., are provided)

```
SELECT ((base_fare + tolls) + (estimated_distance * pricing_per_mile))*tax_rate AS total_fare
FROM fare_estimate
WHERE trip_id = 887766; -- trip_id  would come from App
```


MAKING CHANGES IN THE DATABASE:

ALTER TABLE Trip ADD [CASE] INT;
UPDATE TRIP SET [Case] = ABS(CHECKSUM(NEWID()) % 2);

EXEC sp_rename 'TRIP.[Case]', 'Status', 'COLUMN';

EXEC sp_rename 'TRIP.[STATUS]', 'Status','COLUMN';
Create a free web site with Weebly
  • Home
  • BLOG
  • About Me
  • INTERESTS
    • AI/Machine Learning >
      • Machine Learning
      • Machine Learning_Complete
      • ML|Text2Speech
    • Statistics 4 Business >
      • Survival | Multilevel | GLM
      • Statistics| Max Likelyhood and OLS
      • Probability Distribution Functions
      • Log and Exponential Transformation
      • Heteroskendasticity and Robust Methods
      • Statistics| Basics II
      • Statistics| Basics I
    • Cloud Architecture >
      • AWS Intro >
        • AWS | Hands On 1
      • Cloud Computing
      • Cloud Architecting
      • BIG DATA >
        • MapReduce
        • SPARK
    • Web Development >
      • WEB APP DEV
      • Java Script
      • Java
      • Network Security
    • BIG DATA FOR BUSINESS >
      • SQL
    • Business Analytics >
      • Lift Curves
      • Market Basket Analysis
    • Valuation | Risk Free Rate >
      • Valuation | Example DCW_Part I
      • Valuation | Example DCW_Part II
      • Valuation | The Idea
      • Valuation | Financial Statements
      • Valuation | DCF & Risk Free Rate
      • Valuation|Equity Risk Premium
      • Valuation | Relative Valuation
      • Valuation | Terminal Value
      • Investing
    • Visualizations
    • Skill Set
    • Academics
  • My Apps
  • Articles
    • Engineering Success
    • Why Hire Me
    • My Poems