0% found this document useful (0 votes)
0 views2 pages

SQL MongoDB Cheat Sheet

This document provides a cheat sheet for SQL and MongoDB syntax, covering essential commands such as creating tables, inserting data, selecting with filters, joining tables, and creating views in SQL, as well as database usage, document insertion, and querying in MongoDB. It includes specific examples for each command to illustrate their usage. The content is structured to serve as a quick reference for database operations in both SQL and MongoDB environments.

Uploaded by

moekadi.molefe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views2 pages

SQL MongoDB Cheat Sheet

This document provides a cheat sheet for SQL and MongoDB syntax, covering essential commands such as creating tables, inserting data, selecting with filters, joining tables, and creating views in SQL, as well as database usage, document insertion, and querying in MongoDB. It includes specific examples for each command to illustrate their usage. The content is structured to serve as a quick reference for database operations in both SQL and MongoDB environments.

Uploaded by

moekadi.molefe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

DATABASES CHEAT SHEET: SQL + MongoDB Syntax

QUESTION 3: MySQL Syntax

-- Create Table
CREATE TABLE TableName (
Column1 DataType,
Column2 DataType,
PRIMARY KEY (PrimaryKeyColumn),
FOREIGN KEY (FK_Column) REFERENCES OtherTable(PrimaryKey)
);

-- Insert Data
INSERT INTO TableName (Col1, Col2, Col3)
VALUES (Val1, Val2, Val3);

INSERT INTO TableName VALUES


(1, 'Name', 'Surname'),
(2, 'Another', 'Person');

-- Select with Filter


SELECT * FROM TableName
WHERE Date BETWEEN 'YYYY-MM-DD' AND 'YYYY-MM-DD';

-- Join Tables
SELECT TableA.Col1, TableB.Col2
FROM TableA
JOIN TableB ON TableA.FK = TableB.PK;

-- Count and Group


SELECT Name, COUNT(*) AS Total
FROM TableName
GROUP BY Name
ORDER BY Total DESC;

-- Order by Date
SELECT * FROM TableName
ORDER BY Date DESC;

-- Create View
CREATE VIEW ViewName AS
SELECT Col1, Col2
FROM TableName
WHERE Condition;

-- Stored Procedure
DELIMITER //

CREATE PROCEDURE get_appointments(IN input_date DATE)


BEGIN
SELECT Time, Duration, DoctorName, DoctorSurname, PatientName, PatientSurname
FROM Appointments
JOIN Doctor ON Appointments.DoctorID = Doctor.DoctorID
DATABASES CHEAT SHEET: SQL + MongoDB Syntax

JOIN Patient ON Appointments.PatientID = Patient.PatientID


WHERE Date = input_date
ORDER BY Time ASC;
END //

DELIMITER ;

QUESTION 4: MongoDB Shell Syntax

// Use / Create Database


use clients_s123456

// Insert One or Many


db.clients.insertOne({
ClientName: "Debbie",
ClientSurname: "Theart",
ClientDOB: ISODate("1980-03-17")
});

db.clients.insertMany([
{
ClientName: "Debbie",
ClientSurname: "Theart",
ClientDOB: ISODate("1980-03-17")
},
{
ClientName: "Thomas",
ClientSurname: "Duncan",
ClientDOB: ISODate("1976-08-12")
}
]);

// Show All Documents


db.clients.find();

// Query by Date
db.clients.find({
ClientDOB: { $gt: ISODate("1979-01-12") }
});

You might also like