SQL Daily Practice (Nioke New Question Bank) - Day 9: Retrieving Data

1. Retrieve all IDs from the Customers table

Topic :

insert image description here

Create table statement :


DROP TABLE IF EXISTS `Customers`;

CREATE TABLE IF NOT EXISTS `Customers`(
    cust_id VARCHAR(255) DEFAULT NULL
);

INSERT `Customers` VALUES ('A'),('B'),('C');

Problem solving answer :

select
  distinct cust_id
from
  Customers;

insert image description here

2. Retrieve and make a list of ordered products

Question : The table OrderItems contains a non-empty column prod_id representing the item id, which contains all ordered items (some have been ordered multiple times).
insert image description here

Create table statement :



DROP TABLE IF EXISTS `OrderItems`;
CREATE TABLE IF NOT EXISTS `OrderItems`(
	prod_id VARCHAR(255) NOT NULL COMMENT '商品id'
);
INSERT `OrderItems` VALUES ('a1'),('a2'),('a3'),('a4'),('a5'),('a6'),('a6')

Problem solving answer :

// 1. 去重
select distinct prod_id from OrderItems
 
// 2. 分组
select prod_id from OrderItems group by prod_id

insert image description here

3. Retrieve all columns

Question : Now there is a Customers table (the table contains columns cust_id for customer id, cust_name for customer name)

insert image description here

Create table statement :

DROP TABLE IF EXISTS `Customers`;
CREATE TABLE IF NOT EXISTS `Customers`(
	cust_id VARCHAR(255) NOT NULL COMMENT '客户id',
	cust_name VARCHAR(255) NOT NULL COMMENT '客户姓名'
);
INSERT `Customers` VALUES ('a1','andy'),('a2','ben'),('a3','tony'),('a4','tom'),('a5','an'),('a6','lee'),('a7','hex');

Problem solving answer :

# 匹配所有列
select * from Customers 

# 指定列名
select cust_id,  cust_name from Customers  

insert image description here

4. How to make question-writing more efficient?

Recently, many friends who have learned the basics have asked me how to improve my programming level? After learning the basics, which questions should be brushed on? Obviously I have learned a lot, but I don’t know how to get started with the project. In fact, this is because I practice too little. I only focus on learning, but ignore the problem solving. Only continuous practice can improve and consolidate programming thinking and ability!
insert image description here
Link address : Niuke.com | ! !

Guess you like

Origin blog.csdn.net/yuan2019035055/article/details/126632256