You are on page 1of 3

Schema OR Database [They are considered same for MySQL.

] - A schema could be
considered as a group of related database objects such as tables, views, indexes
etc.
Anything that is followed by hyphen hyphen(i.e. --) is treated as comment in MySQL.
-- Create a new database with name DemoDB and with the character set
-- utf8 which stands for unicode text format 8 which is widely used for supporting
international character set.

CREATE DATABASE `DemoDB` DEFAULT CHARACTER SET `utf8`


NUMERIC(20,4) means 16 digits for integer part and 4 digits for fraction part

Projections means choosing the information that needs to be displayed or selected.


When you have a table you can modify and display the information as per the need,
this is referred to projecting the information. This could be achieved using the
Query(i.e. the SELECT command) itself

To combine two columns while projecting:


In SQL Server,
SELECT col1 + ' ' + Col2 FROM Table-name;

In MySQL
SELECT CONCAT (col1, CONCAT(' ', Col2)) AS Newcolname FROM Table-name;

To get distict column from table


SELECT DITINCT Colname FROM Tablename

Create the table Employee with new structure.

CREATE TABLE `Employee`(


`EmpID` INT,
`FirstName` VARCHAR(50),
`LastName` VARCHAR(50),
`Salary` NUMERIC(20,2),
`CreatedDTTM` DATETIME DEFAULT NOW()
);

INSERT command is used to insert data into the table.


-- Normally when you did not specify the column list then you need to supply the
-- values for all the columns, otherwise it leads to an error.

INSERT INTO `Employee`


VALUES(1, 'A', 'A', 1000, NOW() );

-- You need to mention the column list if you looking to provide


-- values for specific columns. Columns for which values are not
-- provided will take the DEFAULT value if any, otherwise NULL.

-- IMPORTANT
-- NULL is not equal to zero (0) and it is not equal to empty string ('')
-- NULL could be considered as undetermined or the value is not known.

-- In the below command CreatedDTTM takes the default value i.e. NOW()
-- which is the current system date time.

INSERT INTO `Employee`(`EmpID`, `FirstName`, `LastName`, `Salary`)


VALUES(2, 'B', 'B', 1000);

-- With the below insert command CreatedDTTM takes the default value i.e. NOW()
-- but LastName takes NULL as there is no default value given for LastName.
INSERT INTO `Employee`(`EmpID`, `FirstName`, `Salary`)
VALUES(3, 'C', 1000);

-- NULL could also be given as a value explicitly, here the LastName


-- is set to NULL intensionally.

INSERT INTO `Employee`(`EmpID`, `FirstName`, `LastName`, `Salary`)


VALUES(4, 'D', NULL, 1000);

-- Display Employee information

SELECT * FROM `Employee`;

CREATE TABLE Candidate(Name varchar(50), UserID varchar(50), Skills int);


INSERT INTO Candidate('Abraham', 'abc', 70);
INSERT INTO Candidate('Clive', 'def', 90);
INSERT INTO Candidate('Gabriel', 'ghi', 100);
INSERT INTO Candidate('John', 'jkl', 50);

CREATE TABLE Problems (ProblemName varchar(100), Score int);


INSERT INTO Problems('A', 10);
INSERT INTO Problems('B', 20);
INSERT INTO Problems('C', 30);
INSERT INTO Problems('D', 40);
INSERT INTO Problems('E', 50);

CREATE TABLE Submissions(UserID varchar(50), ProblemName varchar(100));


INSERT INTO Submissions('abc', A);
INSERT INTO Submissions('def', B);
INSERT INTO Submissions('ghi', C);
INSERT INTO Submissions('abc', D);
INSERT INTO Submissions('jkl', D);
INSERT INTO Submissions('jkl', E);

select

c.name,

c.UserId,

sum (Score) as Total

from Submissions s

join Candidates c

on s.UserId=c.UserId

join Problems p

on s.ProblemName=p.ProblemName

group by 1,2

having sum (Score)>=50

order by 3 desc

You might also like