Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save VHIzq/8d90a5717f1e649d9aa260a7722711e3 to your computer and use it in GitHub Desktop.
Save VHIzq/8d90a5717f1e649d9aa260a7722711e3 to your computer and use it in GitHub Desktop.
public_sentences.sql
-- SCHEMA
DROP TABLE IF EXISTS employees;
CREATE TABLE IF NOT EXISTS employees (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
hire_at DATE,
salary DECIMAL(10,2) NOT NULL,
status ENUM('active', 'inactive') DEFAULT 'active',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO employees (id, first_name, last_name, email, hire_at, salary, created_at, status)
VALUES
(1, 'John', 'Doe', '[email protected]', '2022-01-10', 50000.00, NOW(), 'active'),
(2, 'Jane', 'Smith', '[email protected]', '2022-02-15', 55000.00, NOW(), 'active'),
(3, 'Michael', 'Johnson', '[email protected]', '2022-03-20', 60000.00, NOW(), 'inactive'),
(4, 'Emily', 'Williams', '[email protected]', '2022-04-25', 52000.00, NOW(), 'active'),
(5, 'William', 'Brown', '[email protected]', '2022-05-30', 48000.00, NOW(), 'active'),
(6, 'Olivia', 'Jones', '[email protected]', '2022-06-05', 53000.00, NOW(), 'active'),
(7, 'James', 'Davis', '[email protected]', '2022-07-10', 55000.00, NOW(), 'active'),
(8, 'Emma', 'Miller', '[email protected]', '2022-08-15', 60000.00, NOW(), 'active'),
(9, 'Benjamin', 'Garcia', '[email protected]', '2022-09-20', 49000.00, NOW(), 'active'),
(10, 'Sophia', 'Martinez', '[email protected]', '2022-10-25', 54000.00, NOW(), 'active'),
(11, 'Liam', 'Rodriguez', '[email protected]', '2022-11-30', 51000.00, NOW(), 'active'),
(12, 'Ava', 'Lopez', '[email protected]', '2022-12-05', 59000.00, NOW(), 'active'),
(13, 'Henry', 'Hernandez', '[email protected]', '2023-01-10', 50000.00, NOW(), 'inactive'),
(14, 'Mia', 'Gonzalez', '[email protected]', '2023-02-15', 55000.00, NOW(), 'active'),
(15, 'Alexander', 'Perez', '[email protected]', '2023-03-20', 60000.00, NOW(), 'active'),
(16, 'Ella', 'Wilson', '[email protected]', '2023-04-25', 52000.00, NOW(), 'active'),
(17, 'Daniel', 'Moore', '[email protected]', '2023-05-30', 48000.00, NOW(), 'active'),
(18, 'Grace', 'Taylor', '[email protected]', '2023-06-05', 53000.00, NOW(), 'active'),
(19, 'Michael', 'Jackson', '[email protected]', '2023-07-10', 55000.00, NOW(), 'active'),
(20, 'Chloe', 'White', '[email protected]', '2023-08-15', 60000.00, NOW(), 'active'),
(21, 'William', 'Harris', '[email protected]', '2023-09-20', 49000.00, NOW(), 'inactive'),
(22, 'Sophia', 'Clark', '[email protected]', '2023-10-25', 54000.00, NOW(), 'active'),
(23, 'Ethan', 'Lewis', '[email protected]', '2023-11-30', 51000.00, NOW(), 'active'),
(24, 'Isabella', 'Walker', '[email protected]', '2023-12-05', 59000.00, NOW(), 'active'),
(25, 'James', 'Young', '[email protected]', '2024-01-10', 50000.00, NOW(), 'active'),
(26, 'Avery', 'Wright', '[email protected]', '2024-02-15', 55000.00, NOW(), 'active'),
(27, 'Logan', 'Martin', '[email protected]', '2024-03-20', 60000.00, NOW(), 'active'),
(28, 'Sophia', 'Hall', '[email protected]', '2024-04-25', 52000.00, NOW(), 'active'),
(29, 'Henry', 'Allen', '[email protected]', '2024-01-19', 48000.00, NOW(), 'active'),
(30, 'Olivia', 'Baker', '[email protected]', '2024-01-19', 53000.00, NOW(), 'active');
SELECT
COUNT(*) AS total
FROM employees
WHERE hire_at = current_date;
SELECT
*
FROM employees
WHERE status = 'active';
-- SUM, MIN, MAX, AVG, COUNT
SELECT
status,
COUNT(*) as total
FROM employees
GROUP BY status;
SELECT 'activo', count(*) from employees where status = 'active'
UNION
SELECT 'inactivo', count(*) from employees where status = 'inactive';
SELECT
*
FROM employees
ORDER BY hire_at DESC
LIMIT 1;
SELECT
*
FROM employees
ORDER BY hire_at ASC
LIMIT 1;
SELECT
*
FROM employees
WHERE hire_at = (SELECT MAX(hire_at) AS max FROM employees);
-- SELECT TOP 1 *
-- FROM employees
-- ORDER BY hire_at DESC;
SELECT
first_name,
last_name
hire_at,
DATEDIFF( NOW(), hire_at) / 365 AS days
FROM employees
ORDER BY days DESC
LIMIT 3;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment