Main Page
 The gatekeeper of reality is
 quantified imagination.

Stay notified when site changes by adding your email address:

Your Email:

Bookmark and Share
Email Notification
Project SQL
Purpose
The purpose of this page is to provide a way for you to complete a variety of SQL queries (T-SQL) with your web browser; there is no longer a need for you to download massive software applications like there was back in the day. This method allows you to prototype queries quickly or simply practice.

In order to perform SQL activity through your web browser, you will need to copy-n-paste a precrafted Schema / DDL (below) and then go to SQLFiddle where you will add it (or if you have time, you can construct your own). SQLFiddle is pretty self-explanatory as to how to use it.

After you have added the Schema / DDL (whether below, or your own) you can begin running queries against it in the other window pane on their page. I have also included a few queries you can copy-n-paste if you decide to use the Schema / DDL on this page.

NOTE: I will point out, once on the SQLFiddle webpage, you can select the SQL engine to use at the top of the page. There are several to choose from but I have only used MySQL and MS SQL 2017 (though, MS SQL 2017 seems to have periodic problems with what seems to me to be centered around virtualization...but I'm just guessing as I don't have access to the back-end).

SCHEMA / DDL:
-- Primary Table.  Intuitively you may want to add a foreign key reference here to emproductsold for all sales per unique employee in this table, but that would 
-- not work.  Instead you have to think in reverse (many to 1 relationship) and specify the foreign key information in the secondary table that will have many 
-- rows tied to a single employee in this primary table.
CREATE TABLE employee (
	id INT NOT NULL,
	empname VARCHAR(25),
	empage INT,
	empgender VARCHAR(1) NOT NULL,
	empsalary INT,
	PRIMARY KEY (id)
);
-- Secondary Table for JOIN queries.  This table has many rows that correspond to a single employee row in the Primary Table.  Notice the foreign key is defined 
-- here and links to the primary table's primary key.
CREATE TABLE emproductsold (
	id INT NOT NULL,
	eid INT NOT NULL,
	salesyear INT NOT NULL,
	salesunitsold INT,
	salestoproduct VARCHAR(25),
	PRIMARY KEY (id),
	FOREIGN KEY (eid) REFERENCES employee (id)
);
-- Populate Primary Table
INSERT INTO employee(id, empname, empage, empgender, empsalary) VALUES (1, 'Snappy', 32, 'm', 20500);
INSERT INTO employee(id, empname, empage, empgender, empsalary) VALUES (2, 'Jimmy', 41, 'm', 18890);
INSERT INTO employee(id, empname, empage, empgender, empsalary) VALUES (3, 'Angie', 24, 'f', 26720);
-- Populate Secondary Table
INSERT INTO emproductsold(id, eid, salesyear, salesunitsold, salestoproduct) VALUES (1, 1, 2020, 5, 'Hot Hat');
INSERT INTO emproductsold(id, eid, salesyear, salesunitsold, salestoproduct) VALUES (2, 2, 2020, 3, 'Paper Ream');
INSERT INTO emproductsold(id, eid, salesyear, salesunitsold, salestoproduct) VALUES (3, 3, 2021, 4, 'Hot Hat');
INSERT INTO emproductsold(id, eid, salesyear, salesunitsold, salestoproduct) VALUES (4, 2, 2021, 6, 'Beakers');
INSERT INTO emproductsold(id, eid, salesyear, salesunitsold, salestoproduct) VALUES (5, 1, 2021, 12, 'Beakers');
INSERT INTO emproductsold(id, eid, salesyear, salesunitsold, salestoproduct) VALUES (6, 3, 2020, 2, 'Paper Ream');


A Few SQL Queries:
-- Get the number of products sold per employee.
SELECT
  e.empname,
  SUM(p.salesunitsold) AS 'Units Sold'
FROM employee e
INNER JOIN emproductsold p ON (e.id = p.eid)
GROUP BY e.empname;

-- Get employee with highest salary; more readable as to what is happening with an additional SELECT.
SELECT
  empname,
  empsalary
FROM employee
WHERE empsalary = (SELECT MAX(empsalary) FROM employee);

-- Get employee with highest salary; limited to single SELECT and aggregate behavior (you get the same result as the query above).
SELECT
  empname,
  MAX(empsalary)
FROM employee;

-- Get average age, by gender, for employees over 21 years old.
SELECT
  empgender,
  AVG(empage) AS 'empage'
FROM employee
GROUP BY empgender
HAVING (AVG(empage) > 21);


About Joe