#1
CREATE TABLE students(
  sid int primary key,
  name varchar(50),
  age int,
  gpa float
);
CREATE TABLE courses(
  cid int primary key,
  deptid varchar(10),
  name varchar(50)
);
CREATE TABLE professors(
  ssn char(10) primary key,
  name varchar(50),
  address varchar(50),
  phone char(12),
  deptid varchar(10)
);
CREATE TABLE teaches(
  cid int,
  section int,
  primary key (cid, section),
  ssn char(10),
  foreign key (cid) references courses(cid),
  foreign key (ssn) references professors(ssn)
);
CREATE TABLE enrollment(
  sid int,
  cid int,
  primary key (sid, cid),
  section int,
  grade char(1),
  foreign key (sid) references students(sid),
  foreign key (cid) references courses(cid),
  foreign key (cid, section) references teaches(cid, section)
);

# dummy data
INSERT INTO students VALUES 
  (1, 'Brandon', 20, 4.0),
  (2, 'Anna', 20, 4.0),
  (3, 'Vineal', 20, 4.0),
  (4, 'brogman', 99, 0),
  (5, 'brogman2', 99, 0),
  (6, 'brogman3', 99, 0);
select * from students;

INSERT INTO courses VALUES
  (1, 'cs', 'Introduction to cs'),
  (2, 'cs', 'Data Structures'),
  (3, 'math', 'Intro to math reasoning'),
  (4, 'math', 'Real *****');
select * from courses;

INSERT INTO professors VALUES
  ('12-345-678', 'Molnar', '123 guy avenue', '123-456-7890', 'math'),
  ('99-345-678', 'Sesh', '1234 soft meth avenue', '999-456-7890', 'cs'),
  ('98-765-432', 'Parth', '999 UML lane', '999-999-9999', 'cs');
select * from professors;

INSERT INTO teaches VALUES
  (1, 1, '99-345-678'),
  (1, 2, '99-345-678'),
  (1, 3, '99-345-678'),
  (2, 1, '99-345-678'),
  (3, 1, '12-345-678'),
  (3, 2, '12-345-678'),
  (4, 1, '12-345-678');
select * from teaches;

INSERT INTO enrollment VALUES
  (1, 1, 2, 'A'),
  (1, 4, 1, 'B'),
  (2, 2, 1, 'A'),
  (2, 3, 1, 'B'),
  (3, 1, 1, 'A'),
  (3, 2, 1, 'A'),
  (4, 1, 1, 'D'),
  (4, 2, 1, 'D'),
  (4, 3, 1, 'F'),
  (4, 4, 1, 'F'),
  (5, 1, 1, 'F'),
  (5, 3, 1, 'F'),
  (6, 1, 1, 'F');
  
select * from enrollment;

#2 name of professors that work for cs
SELECT name
FROM professors
WHERE deptid = 'cs';

#3 students (sid) enrolled in courses in the cs department
SELECT DISTINCT e.sid
FROM enrollment e
JOIN courses c on e.cid = c.cid;

#4 list ssn and name of professors that work for the cs department, but are not teaching any cs courses
SELECT ssn, name
FROM professors
WHERE deptid = 'cs' 
  AND ssn NOT IN (
    SELECT ssn
    FROM teaches t
    JOIN courses c ON t.cid = c.cid
    WHERE c.deptid = 'cs'
  );

#5 list number of courses offered by each department (just number of courses not sections)
SELECT deptid, count(*) numCourses
FROM courses
GROUP BY deptid;

#6 list the departments that offer more than 10 courses.
# (untested)
SELECT t.deptid
FROM (
  SELECT deptid, count(*) numCourses
  FROM courses
  GROUP BY deptid
) t
WHERE t.numCourses > 10;

#7 list of name of students whose professor's name starts with M
SELECT DISTINCT s.name
FROM students s
JOIN enrollment e on s.sid = e.sid
JOIN (
  # courses taught by professors starting with M
  SELECT c.cid
  FROM courses c
  JOIN teaches t on c.cid = t.cid
  JOIN professors p on t.ssn = p.ssn
  WHERE p.name LIKE 'M%'
) t1 on e.cid = t1.cid;

#8 number of small, medium, and large sections per dept
#potential problem: does not account for empty sections
CREATE TEMPORARY TABLE smallMediumLarge
SELECT deptid, 
  SUM(IF(t1.numStudents < 30, 1, 0)) small, 
  SUM(IF(t1.numStudents >= 30 AND numStudents < 80, 1, 0)) medium, 
  SUM(IF(t1.numStudents >= 80, 1, 0)) large
FROM (
  # number of students per (course,section)
  SELECT c.deptid, e.cid, e.section, count(*) numStudents
  FROM enrollment e
  JOIN courses c on e.cid = c.cid
  GROUP BY c.deptid, e.cid, e.section
) t1
GROUP BY deptid;
SELECT * FROM smallMediumLarge;

#9 list of professors that work for departments with more than 20 faculty members 
# and that offer more large sections than small and medium sections combined

SELECT p.name
FROM professors p
WHERE p.deptid IN (
    # table of departments with more than 20 profs
    SELECT DISTINCT deptid
    FROM(
      SELECT p.deptid, count(*) numProfs
      FROM professors p
      GROUP BY p.deptid
    ) t1
    WHERE numProfs > 20
  )
  AND
  p.deptid IN (
  # table of departments with more large sections than small and medium
    SELECT deptid
    FROM smallMediumLarge
    WHERE large > small + medium
  );
  
#10 for each course find percentage of students that failed the course
CREATE TEMPORARY TABLE passPercentTable
SELECT e.cid, e.section, AVG(IF(e.grade = 'A' OR e.grade = 'B' OR e.grade = 'C', 1, 0)) passPercent
FROM enrollment e
GROUP BY e.cid, e.section;
SELECT * FROM passPercentTable;

#11 name of professor with max percentage of students that failed their course
# assuming most fail percent in a single section:
CREATE TEMPORARY TABLE passPercentTable2
SELECT * FROM passPercentTable;

SELECT DISTINCT p.name
FROM professors p
JOIN teaches t on p.ssn = t.ssn
JOIN (
  SELECT cid, section
  FROM passPercentTable2 pt2
  WHERE pt2.passPercent = (
    SELECT DISTINCT MIN(pt1.passPercent)
    FROM passPercentTable pt1
  )
) pt3 on t.cid = pt3.cid AND t.section = pt3.section;

#12 on average what percentage of students fail a course? total number 
# of students that failed / total number enrolled
CREATE TEMPORARY TABLE coursesFailTable
SELECT e.cid, AVG(IF(e.grade = 'D' OR e.grade = 'F', 1, 0)) failPercent
FROM enrollment e
GROUP BY e.cid;

#13 list of courses (sections) where the percentage of students with D/F is greater than average
SELECT AVG(cft.failPercent)
FROM coursesFailTable cft;

SELECT pp.cid, pp.section
FROM passPercentTable pp
WHERE pp.passPercent < (
  SELECT AVG(cft.failPercent)
  FROM coursesFailTable cft
);

#14 SPS table

SELECT * FROM enrollment e JOIN courses c on e.cid = c.cid WHERE c.deptid = 'math';

# total numbers for each course
SELECT c.deptid,
  SUM(numStudents) / SUM(numSections) SPS,
  SUM(numAs)/sum(numStudents) pA,
  SUM(numBs)/sum(numStudents) pB,
  SUM(numCs)/sum(numStudents) pC,
  SUM(numDs)/sum(numStudents) pD,
  SUM(numFs)/sum(numStudents) pF
FROM courses c
JOIN (
  #total numbers for each course
  SELECT t.cid, SUM(t.numStudents) numStudents, COUNT(*) numSections,
    SUM(numAs) numAs, SUM(numBs) numBs, SUM(numCs) numCs, SUM(numDs) numDs, SUM(numFs) numFs
  FROM (
    # total numbers for each section
    SELECT e.cid, e.section, count(*) numStudents, 
      SUM(IF(e.grade = 'A', 1, 0)) numAs,
      SUM(IF(e.grade = 'B', 1, 0)) numBs,
      SUM(IF(e.grade = 'C', 1, 0)) numCs,
      SUM(IF(e.grade = 'D', 1, 0)) numDs,
      SUM(IF(e.grade = 'F', 1, 0)) numFs
    FROM enrollment e
    GROUP BY e.cid, e.section
  ) t
  GROUP BY t.cid
) t2 on c.cid = t2.cid
GROUP BY c.deptid;




 

MySQL online editor

Write, Run & Share MySQL queries online using OneCompiler's MySQL online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for MySQL. Getting started with the OneCompiler's MySQL editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'MySQL' and start writing queries to learn and test online without worrying about tedious process of installation.

About MySQL

MySQL is a open-source, free and very popular relational database management system which is developed, distributed and supported by Oracle corporation.

Key Features:

  • Open-source relational database management systems.
  • Reliable, very fast and easy to use database server.
  • Works on client-server model.
  • Highly Secure and Scalable
  • High Performance
  • High productivity as it uses stored procedures, triggers, views to write a highly productive code.
  • Supports large databases efficiently.
  • Supports many operating systems like Linux*,CentOS*, Solaris*,Ubuntu*,Windows*, MacOS*,FreeBSD* and others.

Syntax help

Commands

1. CREATE

CREATE TABLE table_name (
                column1 datatype,
                column2 datatype,
                ....);

Example

CREATE TABLE EMPLOYEE (
  empId INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  dept TEXT NOT NULL
);

2. ALTER

ALTER TABLE Table_name ADD column_name datatype;

Example

INSERT INTO EMPLOYEE VALUES (0001, 'Dave', 'Sales');

3. TRUNCATE

TRUNCATE table table_name;

4. DROP

DROP TABLE table_name;

5. RENAME

RENAME TABLE table_name1 to new_table_name1; 

6. COMMENT

Single-Line Comments:

 --Line1;

Multi-Line comments:

   /* Line1,
   Line2 */

DML Commands

1. INSERT

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

Note: Column names are optional.

Example

INSERT INTO EMPLOYEE VALUES (0001, 'Ava', 'Sales');

2. SELECT

SELECT column1, column2, ...
FROM table_name
[where condition]; 

Example

SELECT * FROM EMPLOYEE where dept ='sales';

3. UPDATE

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition; 

Example

UPDATE EMPLOYEE SET dept = 'Sales' WHERE empId='0001'; 

4. DELETE

DELETE FROM table_name where condition;

Example

DELETE from EMPLOYEE where empId='0001'; 

Indexes

1. CREATE INDEX

  CREATE INDEX index_name on table_name(column_name);
  • To Create Unique index:
  CREATE UNIQUE INDEX index_name on table_name(column_name);

2. DROP INDEX

DROP INDEX index_name ON table_name;

Views

1. Create a View

Creating a View:
CREATE VIEW View_name AS 
Query;

2. How to call view

SELECT * FROM View_name;

3. Altering a View

ALTER View View_name AS 
Query;

4. Deleting a View

DROP VIEW View_name;

Triggers

1. Create a Trigger

CREATE TRIGGER trigger_name trigger_time trigger_event
    ON tbl_name FOR EACH ROW [trigger_order] trigger_body
/* where
trigger_time: { BEFORE | AFTER }
trigger_event: { INSERT | UPDATE | DELETE }
trigger_order: { FOLLOWS | PRECEDES } */

2. Drop a Trigger

DROP TRIGGER [IF EXISTS] trigger_name;

Stored Procedures

1. Create a Stored Procedure

CREATE PROCEDURE sp_name(p1 datatype)
BEGIN
/*Stored procedure code*/
END;

2. How to call Stored procedure

CALL sp_name;

3. How to delete stored procedure

DROP PROCEDURE sp_name;

Joins

1. INNER JOIN

SELECT * FROM TABLE1 INNER JOIN TABLE2 where condition;

2. LEFT JOIN

SELECT * FROM TABLE1 LEFT JOIN TABLE2 ON condition;

3. RIGHT JOIN

SELECT * FROM TABLE1 RIGHT JOIN TABLE2 ON condition;

4. CROSS JOIN

SELECT select_list from TABLE1 CROSS JOIN TABLE2;