天天看點

SQL 入門計劃-1-選擇

從這裡開始,将學習如何掌握Leetcode中的SQL題目,每道題目都會有原本和變式兩套版本,鞏固學習,在實踐中提高我的能力。

1 選擇

原始版本:

SQL 入門計劃-1-選擇

是以,該題目的解答應該如下所示:

第一步:

建立資料庫

SQL 入門計劃-1-選擇

第二步,

建立資料表:

SQL 入門計劃-1-選擇
CREATE TABLE world(
   name   INT NOT NULL AUTO_INCREMENT,
   continent  VARCHAR(100) ,
   area INT NOT NULL ,
   population INT NOT NULL ,
   gdp INT NOT NULL ,
   PRIMARY KEY (name)
   )ENGINE=InnoDB DEFAULT CHARSET=utf8;      

在這裡,SQL 中主鍵name的數值類型和真實數值類型不一緻。

插入相關資料

SQL 入門計劃-1-選擇
INSERT INTO world
     (name, continent, area, population, gdp)
     VALUES
     (1, "a", 3000000,25000000,10);

INSERT INTO world
     (name, continent, area, population, gdp)
     VALUES
     (2, "a", 1000000,15000000,10);

INSERT INTO world
     (name, continent, area, population, gdp)
     VALUES
     (3, "a", 4000000,45000000,10);

INSERT INTO world
     (name, continent, area, population, gdp)
     VALUES
     (4, "a", 5000000,65000000,10);

INSERT INTO world
     (name, continent, area, population, gdp)
     VALUES
     (5, "a", 1000000,5000000,10);

INSERT INTO world
     (name, continent, area, population, gdp)
     VALUES
     (6, "a", 1000000,25000000,10);

INSERT INTO world
     (name, continent, area, population, gdp)
     VALUES
     (7, "a", 13000000,14000000,10);      

寫查詢SQL的語句,得到回報結果。

SQL 入門計劃-1-選擇

查詢結果