SELECT * FROM products;
SELECT title, price FROM products
WHERE price >= 30;
SELECT * FROM customers
ORDER BY name ASC
LIMIT 5;
SELECT COUNT(*) AS anzahl_produkte,
MIN(price) AS billigstes,
MAX(price) AS teuerstes,
AVG(price) AS durchschnitt
FROM products;
SELECT o.order_id,
c.name AS kunde,
p.title AS produkt,
oi.qty,
(oi.qty * p.price) AS position_summe
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
JOIN order_items oi ON oi.order_id = o.order_id
JOIN products p ON p.product_id = oi.product_id
ORDER BY o.order_id;
CREATE DATABASE schooldb;
USE schooldb;
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
class_level VARCHAR(20)
);
CREATE TABLE exams (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(120) NOT NULL,
max_points INT NOT NULL CHECK (max_points > 0)
);
CREATE TABLE results (
student_id INT NOT NULL,
exam_id INT NOT NULL,
points INT NOT NULL CHECK (points >= 0),
PRIMARY KEY (student_id, exam_id),
FOREIGN KEY (student_id) REFERENCES students(id),
FOREIGN KEY (exam_id) REFERENCES exams(id)
);
INSERT INTO students (name, class_level) VALUES
('Frieda', '10A'), ('Matthew', '10B'), ('Alice', '10A');
INSERT INTO exams (title, max_points) VALUES
('Mathe KA1', 60), ('Informatik KA1', 50);
INSERT INTO results (student_id, exam_id, points) VALUES
(1,1,48), (1,2,42), (2,1,30), (3,2,35);
SELECT s.name,
e.title,
r.points,
ROUND(r.points * 100.0 / e.max_points, 2) AS prozent
FROM results r
JOIN students s ON s.id = r.student_id
JOIN exams e ON e.id = r.exam_id;
UPDATE exams SET max_points = max_points + 10 WHERE id = 2;
DELETE FROM results WHERE student_id = 2 AND exam_id = 1;
SELECT e.title,
AVG(r.points) AS avg_points
FROM results r
JOIN exams e ON e.id = r.exam_id
GROUP BY e.title;