MySQL是一種常見的關系型數據庫,SQL查詢是MySQL中最常用的功能之一。下面是一些常見的MySQL SQL查詢面試題。
1. 如何獲取一張客戶表中最近登錄的10個客戶的名稱和登錄時間?
SELECT name, login_time FROM customer ORDER BY login_time DESC LIMIT 10;
2. 如何計算訂單表中每個客戶的訂單數?
SELECT customer_id, COUNT(*) AS num_orders FROM orders GROUP BY customer_id;
3. 如何獲取銷售額前10名的產品名稱和銷售額?
SELECT product_name, SUM(quantity * price) AS sales FROM order_items JOIN products ON order_items.product_id = products.id GROUP BY product_id ORDER BY sales DESC LIMIT 10;
4. 如何獲取每個部門最高工資的員工姓名和工資?
SELECT e.name, e.salary FROM employees e JOIN ( SELECT department_id, MAX(salary) AS max_salary FROM employees GROUP BY department_id ) em ON e.department_id = em.department_id AND e.salary = em.max_salary;
5. 如何獲取每個月的銷售總額?
SELECT DATE_FORMAT(order_date, '%Y-%m') AS month, SUM(quantity * price) AS sales FROM order_items JOIN orders ON order_items.order_id = orders.id GROUP BY month;
以上是一些常見的MySQL SQL查詢面試題,希望能對你的面試有所幫助。