[TOC]
§Problems
§Basic
1 | SELECT DISTINCT Email |
183. Customers Who Never Order
1 | SELECT c.Name AS Customers |
184. Department Highest Salary
1 | SELECT d.Name AS Department, e.Name AS Employee, e.Salary |
1 | DELETE p1 |
1 | SELECT w1.Id |
§JOIN
1 | SELECT p.FirstName, P.LastName, A.City, A.Sate |
181. Employees Earning More Than Their Managers
1 | SELECT a.Name AS Employee |
§Rank
1 | SELECT Max(Salary) AS SecondHighestSalary |
1 | CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT |
1 | # Write your MySQL query statement below |
1 | SELECT DISTINCT l1.Num as ConsecutiveNums |
1 | -- using user-defined variables |
185. Department Top Three Salaries
1 | SELECT d.Name AS Department, e.Name AS Employee, e.Salary AS Salary |
§Basic knowledge
§Links
§SELECT - extracts data from a database
1 | SELECT column1, column2, ... |
1 | # SQL Server / MS Access Syntax: |
1 | MIN(), MAX(), COUNT(), AVG(), SUM() |
§UPDATE - updates data in a database
1 | UPDATE table_name |
§DELETE - deletes data from a database
1 | DELETE FROM table_name |
§INSERT INTO - inserts new data into a database
1 | # If only insert in specified columns, others = null |
§ALIASE - give a table, or a column in a table, a temporary name
1 | 1. |
§JOIN - combine rows from two or more tables, based on a related column between them
1 | SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate |
-GROUP BY statement - used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.
1 | SELECT COUNT(CustomerID), Country |
§Operators
1 | UNION, UNION ALL with duplicate values |
§Comments
1 | -- Single line |
§DATABASE
- CREATE DATABASE - creates a new database
- DROP DATABASE - drop an existing SQL database
- ALTER DATABASE - modifies a database
§TABLE
- CREATE TABLE - creates a new table
1 | CREATE TABLE table_name ( |
- DROP TABLE - deletes a table
- ALTER TABLE - modifies a table
1 | ALTER TABLE table_name |
- CREATE INDEX - creates an index (search key)
- DROP INDEX - deletes an index
- …