Fundamental of Database Group Work
Fundamental of Database Group Work
A graph database (GDB) is a database that uses graph structures for storing data. It
uses nodes, edges, and properties instead of tables or documents to represent and store
data. Ex: Neo4j, Amazon Neptune, ArangoDB etc.
The edges represent relationships between the nodes. This helps in retrieving data
more easily and, in many cases, with one operation. Graph databases are commonly
referred to as NoSQL.
Graph databases are similar to 1970s network model databases in that both represent
general graphs, but network-model databases operate at a lower level of abstraction
and lack easy traversal over a chain of edges.
The underlying storage mechanism of graph databases can vary. Relationships are
first-class citizens in a graph database and can be labelled, directed, and given
properties.
2. When relationships between data elements are more important For example-
there is a profile and the profile has some specific information in it but the major
selling point is the relationship between these different profiles that is how you
get connected within a network.
In the same way, if there is data element such as user data element inside a graph
database there could be multiple user data elements but the relationship is what is
going to be the factor for all these data elements which are stored inside the graph
database.
3. Low latency with large scale data When you add lots of relationships in the
relational database, the data sets are going to be huge and when you query it, the
complexity is going to be more complex and it is going to be more than a usual time.
However, in graph database, it is specifically designed for this particular purpose and
one can query relationship with ease.
Flexible Schema: They allow a schema-less structure, meaning you can evolve
your data model without costly migrations, making them ideal for dynamic
applications.
Relationships:
(:Student)-[:ENROLLED_IN]->(:Course)
(:Professor)-[:TEACHES]->(:Course)
Create Nodes:
CREATE (alice:Student {name: 'Alice', student_id: 'S001'})
CREATE (bob:Student {name: 'Bob', student_id: 'S002'})
CREATE (math:Course {title: 'Mathematics', course_code: 'MATH101'})
CREATE (prof:Professor {name: 'Dr. Smith', department: 'Mathematics'})
Create Relationships:
CREATE (alice)-[:ENROLLED_IN]->(math)
CREATE (bob)-[:ENROLLED_IN]->(math)
CREATE (prof)-[:TEACHES]->(math)
Sample Queries
1. Find all students enrolled in Mathematics:
MATCH (s:Student)-[:ENROLLED_IN]->(c:Course {title: 'Mathematics'})
RETURN s.name
What is MongoDB?
MongoDB is a document-oriented NoSQL database designed for storing and
managing large volumes of unstructured or semi-structured data. Unlike traditional
relational databases, MongoDB uses a flexible schema design, allowing developers to
store data in a way that aligns seamlessly with modern application requirements.
Key Features
Indexing: Every field in a MongoDB document can be indexed, making data retrieval
faster and more efficient
Architecture
Programming language accessibility
MongoDB has official drivers for major programming languages and development
environments. There are also a large number of unofficial or community-supported
drivers for other programming languages and frameworks.
Serverless access
Management and graphical front-ends
1. Insert an order
db.orders.insertOne({
order_id: "ORD123",
customer_name: "Robel",
items: [
{ product: "Laptop", qty: 1, price: 1000 },
{ product: "Mouse", qty: 2, price: 25 }
],
total: 1050,
status: "Shipped",
order_date: new Date()
})
db.orders.updateOne(
{ order_id: "ORD123" },
{ $set: { status: "Delivered" } }
)
4. Find orders with product "Laptop"
db.orders.find({ "items.product": "Laptop" })
Operator Description
$gt Greater than
$lt Less than
$set Updates the value of a field
$in Matches any value in an array
$push Adds an item to an array
$and / $or Combines conditions
Large-scale data storage for machine learning, analytics, and data pipelines.Advanced
querying with joins, subqueries, and window functions for powerful data exploration.
Data transformation using complex SQL operations for data cleaning and preparation.
In-database analytics using functions, triggers, and procedures to compute and
automate logic directly in the database.
Examples
E-commerce Inventory Management: A retail company uses Postgress database to
track products, stock levels, and sales. The database helps in real-time inventory
updates and generating sales reports.
Sample Queries
Creating a Table:
name VARCHAR(100),
email VARCHAR(100),
join_date DATE );
Inserting a data:
Updating Data:
Deleting Data:
Inner Join
The INNER JOIN clause in SQL is used to combine records from two or more tables.
The result contains only the rows that have matching values in both tables based on a
specific condition. This makes INNER JOIN a valuable tool when we need to work
with related data across multiple tables in a database.
The key feature of an INNER JOIN is that it filters out rows from the result where
there is no matching data in both tables. Essentially, it returns a “subset” of the data
where the condition is satisfied.
Use Case: Inventory managers reorder stock for products with confirmed supplier
records.
Example: Join products and suppliers tables on supplier_id to get product and supplier
details.
Sample Queries
Sample Tables:
employees (employee_id, name, department_id)
departments (department_id, department_name)
Sample Tables:
CREATE TABLE customers(
customer_id INT,
first_name VARCHAR(50),
last_name VARCHAR(50)
);
CREATE TABLE orders(
order_id INT,
amount INT,
customer INT
);
Customers table:
Customer_id First_nam Last_name
e
1 Natnael Melaku
2 Biruk Dagim
3 Minase Tesfaye
4 Yosef Mulatu
Orders table:
ordre_id Amoun Customer
t
1 50 10
2 100 4
3 86 6
4 49 2
Query:
SELECT customer_id, first_name, amount
FROM customers
FULL OUTER JOIN orders
ON customers.customer_id = orders.customer;
Output:
Customers 2 and 4 have matching values so the amount value is given accordingly.
Customers with ID 1 and 3 have no match in the customer column of orders as a
result the amount is set to NULL. Similarly, customer 10 and 6 are not registered on
the customers table. Therefore, the customer_id and first_name are set to NULL.
From this table, we can filter and identify customers without purchases or purchases
without registered customers.
Left Join
Definition: A LEFT JOIN returns all rows from the left table and the matched rows
from the right table. If there is no match, the result will contain NULL values from the
right table. It is mostly used when you want to get everything from the main table,
whether or not there's a related record in the joined table.
Example:
student_id name
1 Saba
2 Henok
3 Melat
student_id course
1 Math
2 Physics
SQL Query:
SELECT Students.name, Course_Enrollments.course
FROM Students
ON Students.student_id = Course_Enrollments.student_id;
Output:
name course
Saba Math
Henok Physics
Melat NULL
Explanation:
Right Join
Definition: A RIGHT JOIN returns all rows from the right table and the matched
rows from the left table. If there is no match, it will return NULL values from the left
table. It’s useful when we want to get everything from the secondary table, even if it
doesn’t have corresponding data in the main table.
Example:
Now let’s say we want to get a list of all courses that students enrolled in, including
those that don’t have a matching student (e.g., missing or unregistered).
Course_Enrollments Table:
student_id course
1 Math
2 Physics
4 English
Students Table:
student_id name
1 Saba
2 Henok
3 Melat
Conclusion
Understanding Left Join and Right Join is essential when working with relational
databases because not all data is always perfectly matched between tables. These joins
help us uncover missing or unmatched information, which is especially important in
real-world systems like student records, inventory tracking, financial reporting, or
customer databases.
Left Join is useful when we want to see all records from the main (left) table,
even if there are no matching entries in the related table.
Right Join is the opposite—it helps us view everything from the secondary (right)
table, whether or not it has related data in the main table.
REFERENCES
1. What is Graph Database – Introduction | GeeksforGeeks
2. Graph database - Wikipedia
3. Davis Kerby. "Why MongoDB is the way to go". DZone. Archived from the
original on June 12, 2018. Retrieved July 6, 2017.
4. MongoDB - Wikipedia