Most voted "sql" questions
Structured Query Language (SQL) is a language to query databases. Questions should include examples of table code and structure. This tag refers to the default language, not to be used for questions about the implementation of specific DBMS (such as Mysql, Postgresql, Oracle, MS SQL Server). In this case use the specific DBMS tag. Answers to questions marked with ANSI SQL should use SQL as much as possible.
Learn more…6,771 questions
Sort by count of
-
4
votes1
answer2068
viewsCOUNT and GROUP BY in two columns
Personal I am facing a doubt with a particular query, I have searched everywhere on the subject but without success. If there’s any other way to do what I’m trying, I’d appreciate the information So…
-
4
votes2
answers1593
viewsHow to insert sequence in non-automated field
I need to insert records in a table with data from another continuing the sequence of a column that already exists in it, but the column I want to continue the sequence is not auto-incremented and…
-
4
votes1
answer416
viewsHow to group Counts for different queries into one?
I need to make an SQL query to count indications, I have the following querys: SELECT 'FACEBOOK', COUNT(id) FROM `clientes` where indicacao like '%face%' SELECT 'Instagram', COUNT(id) FROM…
-
4
votes1
answer5022
viewsMysql command alter table, difference from "Modify" and "change"?
What are the main differences in change and Modify, in which cases should I use them ? alter table cadastro change nome nome varchar(20); alter table cadastro modify nome varchar(30);…
-
4
votes1
answer1340
viewsWhat are the differences between the HASH and BTREE algorithms used in an index?
I realized that I can create two types of indexes for one determining field in Heidisql, which use the algorithm HASH or BTREE, see below: See the CREATE code from an example table for the…
-
4
votes1
answer1359
viewsPostgresql md5 encryption
I am creating a table using Postgresql and there will still be a password field with MD5 encryption. What would the syntax look like in it? Because I have more contact with Mysql than Postgresql.…
-
4
votes2
answers376
viewsSELECT displaying products except the results of another SELECT
I have this SELECT that picks up the 6 best selling products: SELECT IdProduto, SUM(QtdProdutoPedido) as QtdProdutoPedido FROM tb_pedidoproduto GROUP BY IdProduto ORDER BY QtdProdutoPedido DESC…
-
4
votes1
answer85
viewsDoes using CTE (Common Table Expression) create a type of "cache" in the database?
I have an appointment with several JOIN and queries, and when executed, it takes about 7 seconds to return. Getting the same result, using CTE’s, the query takes around 8 seconds of the first…
-
4
votes3
answers85
viewsHow do you adjust my query date?
$result_usuario = "SELECT * FROM vendas WHERE DATE(date_created) = '$data' AND tipo_venda = '$tipo_vendas' AND tipo = '$tipo_pagamento' ORDER BY 'id'"; I have this query, but it doesn’t work right…
-
4
votes1
answer618
viewsPicking Gaps in an SQL sequence
Hello, today I own a select that returns me the following result: I do another select that returns a value, 5 for example. I would need to know which numbers are not between number 1 and number 5.…
-
4
votes2
answers213
viewsHow to filter only the last day of each month?
How can I filter a query bringing the result by the last day of each month, in SQL Server 2012? I tried the EOMONTH, I think due to the version. SELECT E8_FILIAL, E8_AGENCIA, E8_CONTA, E8_DTSALAT,…
-
4
votes1
answer715
viewsHow to pass parameters for IN clauses in Jasperreports?
I have a question in the passage of a parameter in Ireport. I need to pass a list as a parameter. Example: In SQL code the query looks like this: SELECT * FROM TABELA WHERE COD IN (1,2,3,4) In…
-
4
votes1
answer2217
viewsHow to Bring the 2 highest values of each different id - SQL
I put together a spreadsheet to facilitate the understanding of my doubt. How to select only the 2 "two" largest room house tuition? I only managed to bring the highest value of each room, I wanted…
-
4
votes1
answer155
viewsHow to stop the SQL Server service using C#
I wonder if it is possible to stop SQL Server using C lines of code#. I have a C# application and would like to add a button to stop the SQL service or the instance, so I can work with the files.…
-
4
votes3
answers485
viewsAid in SELECT DISTINCT
I have the following select: SELECT CD_CLIENTE, DT_ATUALIZACAO, FROM TABELA Return me the following result: But I’d like to select just the last DT_ATUALIZAÇÃO of each customer, I thought to use a…
-
4
votes2
answers1851
viewsWhat is the problem of using [n]varchar(max)?
Why the practice of using [n]varchar(max) can cause problems? In my scripts whenever I don’t really know what the length of a string and to prevent problems, I have used nvarchar(max). A few days…
-
4
votes3
answers1332
viewsFormat DATETIME SQL Server
I wonder if you have any way to change the Datetime format in SQL Server EX: 2019-07-09 11:42:00 for 09-07-2019 11:42:00
-
4
votes2
answers229
viewsSQL Maximum value of another table
I am doing a project of the course and I stopped at the last step and I would like a little help from you.. The project is an SQL database with "leasing" (contract for "rental" of cars.) The tables…
-
4
votes3
answers47
viewsHow to view a given value in a table
I’m new to the Sql language, and I wanted to show you the names of all the people who are from the RS region, how could I do that ? create database uri default character set utf8 default collate…
sqlasked 6 years, 7 months ago rafael marques 939 -
4
votes1
answer106
viewsData Manipulation
I wonder if it is possible, after converting the date and bringing only the time, to manipulate this result to return the closed time. The example below details better. CONVERT(VARCHAR(19),…
-
4
votes1
answer93
viewsData Cleanup - SQL Server
I’m working with a column of phones, but some lines have two phones separated by ";". 99999999 ; 99999222 12345678 ; 87654321 40028922 123123456 ; 1122334455 My goal is to put duplicate phones on…
-
4
votes2
answers299
viewsData Modeling: Integrity x Performance
In the company that I work for, there’s a data architecture that I’ve never seen before, and I’d like to know if this is common, or if it’s a new market trend. Just to mention the bank is Oracle.…
-
4
votes2
answers112
viewsHow to make a NOT IN mysql with 2 tables
I have 3 tables Person, class, Notes, table person has: +------+--------------+-----------+ | id | Nome | turma | +------+--------------+-----------+ | 1 | Aluno 01 | 1 | | 2 | Aluno 02 | 2 | In the…
-
4
votes1
answer1240
viewsConvert Unix Timestamp to Datetime
I am trying to select the largest date from my table, however the dates are stored in Unix Timestamp format in a whole column type. I need to do this conversion because I need to calculate the…
-
4
votes4
answers327
viewsReturn results for all months of the interval
I own the following query: SELECT CAST(SUM(CASE WHEN s2.name LIKE '%Servidores' THEN (ta2.time_unit * 1.5)/60 ELSE ta2.time_unit/60 END) as DECIMAL(12,2)) AS TEMPO_CORRETO,…
-
4
votes3
answers902
viewsRETURN THE SUM OF MAXIMUM 3 SQL VALUES
I have a table and I need to return the following: check based on recipient column, add the 3 largest amount of that recipient, bring in query only if the sum of the 3 largest ones are >= 1024:…
-
4
votes2
answers49
views -
4
votes1
answer97
viewsgroup by semester/year PHP
Hello, I have a table with date field, I want to group by semester/year, I did so. SELECT SUM(quantidade) quantidade, YEAR(DATA) ano FROM tbmanipulacao GROUP BY YEAR(DATA), MONTH(DATA) <=6,…
-
4
votes1
answer2755
viewsGROUP BY in query with column by subquery
I have the following select: SELECT A.CARRO, (SELECT TOP 1 VALOR FROM tab2 B WHERE B.DATA <= A.DATA ORDER BY B.DATA DESC) VALOR FROM tab1 A In (SELECT TOP 1 VALOR FROM tab2 B WHERE B.DATA <=…
-
4
votes2
answers3999
viewsHow to define the SET CLIENT_ENCODING = UTF8 permanently?
I am using Postgresql in my DATABASE Accent information is being recorded with accent problems. Ex: It records like it is. Shark records as Shark The encoding of my DATABASE and my server are using…
-
4
votes1
answer385
viewsHow to PIVOT a column by concatenating strings into Sqlserver
Hello, I’m making a query in a database to identify all the columns that are primary key's of the tables of a database and also identify whether or not they are identity. For this I use the query…
-
4
votes1
answer784
viewsSelect the first row of each group in Mysql
Suppose I have such a table: item | group -------+-------- item a | group x item b | group y item c | group y item d | group x item e | group z And that I want to select a single item to represent…
-
4
votes1
answer55
viewsHow to make a select based on data from another table
I have to make the following adaptation in a query: Table app_usuários: id, nome, sobrenome, hash, sexo, interesse, latitude e longitude. Table app_likes: id, user_a_id, user_b_id Briefly, I have a…
-
4
votes2
answers106
viewsHelp with select and repeat records
I have a postgresql database with a table like this: id_usuario | dt_atualizacao | atualizacao | outros_dados 123 | 01-01-2001 | 0 | abc 123 | 01-01-2005 | 1 | abe 123 | 01-01-2012 | 1 | acd 123 |…
-
4
votes1
answer2433
viewsPostgresql - Partial Foreign Key of Composite Primary Key
Fala galera, I need to create a structure model where all my tables have primary key composed by id and version, so I can have multiple versions for each id. Problem: CREATE TABLE table1 { id…
-
4
votes2
answers160
viewsSQL Procedures: Can I use a table as a parameter for a procedure?
I’m a beginner in procedures. I am trying to insert data into a table using a procedure, where I get the table name, the column names and the values of these. How to receive the types of data I…
-
4
votes2
answers1687
viewsPL/SQL Anonymous block | PLS-00103: Encountered the Symbol "CREATE" when expecting one of the following:
I get the following error : [Err] ORA-06550: line 7, column 2: PLS-00103: Encountered the symbol "CREATE" when expecting one of the following: 'Cause I’m using a create Quence inside my BEGIN with ;…
-
4
votes2
answers404
viewsQuery with Datetime Sql and C#
I’m trying to get you to return to me the patient of a particular day and time in command next: SqlCommand cmdSelect = new SqlCommand("select DISTINCT P.nome,P.codPaciente, M.codMedico, C.descricao…
-
4
votes1
answer128
viewsHow to insert records via cursor?
I need to insert unique records by a field that has the unique identifier, so I’m using cursor; however, the unique identifier does not add according to the inserts. ERROR: You are duplicating and…
-
4
votes2
answers173
viewssql record in tables and return the table name
Well, here’s the thing query Mysql; SELECT idMembro FROM membros WHERE idMembro IN ( SELECT idMembro FROM pastores union SELECT idLider FROM redes union SELECT idLider FROM regioes union SELECT…
-
4
votes1
answer332
viewsCompare data from two SQL Server tables
Good night, I am studying SQL queries and I came across a problem in a query, I have two tables: Table: Products +----------------+--------------+--------+ | Codigo | Produto | Valor |…
-
4
votes1
answer75
viewsDoubt query in SQL Server - Information Association
My friends, good night! I need to perform a query in a database using SQL Server and I got stuck in a problem so I ask, please help from you. Here is an example summary of the problem: I have a…
-
4
votes2
answers97
viewsSearch for lower values of one field according to values of another
I have this data entered in my table. mysql> SELECT * FROM db_cotacao; +----+------------+-------+------------+--------+---------+ | id | data | valor | validade | idforn | idativo |…
-
4
votes2
answers613
viewsNodejs - Mysql Error: Too Many Connections
I recently made a refactor of my code to adopt the MVC standard, because it was very disorganized! However, an error called "Too Many Connections" started to appear. This error causes my Node-mysql…
-
4
votes3
answers98
viewsSort select displaying something at the end after sorting
I have a table called Tabela1 and another call Tabela2: Tabela 1 Tabela2 Indice, pessoa_id sts, pessoa_id The index goes from 0 to 10, and sts can be ON-LINE, OFF-LINE OR ON-LINE-PAUSADO. I wish…
-
4
votes1
answer1085
viewsHow to define a composite key table in Sqlite?
I am making an embedded database as a field medium in the integration between two systems. The system A, where you have the data, export all your data to the system B already properly mapped.…
-
4
votes3
answers91
viewsSort by SQL numbering
I have a table with columns ID and ReqNorma filled with a following standard: When I perform the query to filter them, I receive values that are not in order because some of them were registered out…
sqlasked 5 years, 6 months ago Hudson Medeiros 527 -
4
votes2
answers72
viewsKnow which values within the IN condition are not linked to the table
Hello, I have a very simple table as an example: I also have a query that checks the existing values within the IN, example: SELECT * FROM tLetras l WHERE l.letras IN…
-
4
votes1
answer111
viewsCopy column data from one table to another
I have 2 tables "Orcamento" -> colunas ['id', 'cliente_id'] Essa tabela existe valores em 'cliente_id' "Ordem_servicos" -> colunas ['id', 'cliente_id', 'orcamento_id'] essa tabela não existe…
-
4
votes2
answers1661
viewsLaravel: Cannot add Foreign key Constraint
I have two tables, the table users and empresas, with Model User and Empresa respectively. A user registration can manage a company, and for that, I need to define which id the user will access in…