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
-
3
votes1
answer552
viewsUpdate command does not work
I have three tables in a database, and I have a method called alteraLinhaTabela where I try to update one of the three tables at a time by identifying it by the variable tabela. I also pass two more…
-
3
votes2
answers80
viewsHow to create a condition that does not include the month of December?
I am creating a condition to be executed before the month of December, IE, only runs in the period of january to november year-independent. IF (SELECT TarFechamento FROM Tarefa WHERE TarID =…
-
3
votes2
answers6454
viewsHow to use Sum with subquery
How can I use the sum with a subquery inside? I tried many ways, but I couldn’t. When I try to put the sum shortly after the select main, message appears that I cannot use aggregation function with…
-
3
votes2
answers2319
viewsHow to use like in a comparison of fields in different tables?
How can I use the LIKE for a comparison of two different table fields? I need to compare the first 5 characters of each field. I tried with SUBSTRING and with LEFT, however, the performance gets…
-
3
votes1
answer485
viewsSearch the bank in real time
How can I do in PHP so that when the user type in the search field it already appears the database records. For example, assuming that it is a register of cities, the user type "Field", should…
-
3
votes1
answer111
viewsUsing sql_cache in Mysql query
When I learned Mysql, I was recommended to use the expression sql_cache after the SELECT, something like that: "SELECT sql_cache * FROM tabela..." Since then I started to use this in my codes, but…
-
3
votes1
answer83
viewsSearching nearby locations with DISTINCT? (Maps API)
I have a separate table in MYSQL with user addresses containing location information (latitude and longitude), and each user can have more than one registered address. My goal is that when…
-
3
votes3
answers701
viewsMake a search independent of the order of the keywords
I have a database in which I need to do a search based on the keywords, regardless of the order. See the complete table below: +----+-----------------------+ | id | description |…
-
3
votes1
answer243
viewsJOB with 2 Steps, first step stops when reaches 1 minute of execution
I have a JOB configured in SQL to run every 30 minutes, in this has 2 steps, 1 Process in each step, but when the first step reaches 1 minute of execution, it ends without executing all records that…
-
3
votes2
answers999
viewsWhich is more efficient, perform multiple queries or use JOIN?
I have a table X that has my user data, and I need to return to the client the data related to that user in the table Y and Z. I can do it using JOIN or: SELECT attr1, attr2 FROM Y WHERE X_id = id…
-
3
votes2
answers8186
viewsMysql using variables in select
I already searched in some tutorials but I do not find a specification of how to declare the variable in select by Mysql, follows what I am trying to do. DECLARE @idPedido INT; DECLARE @idCombo INT;…
-
3
votes1
answer1227
viewsGet the previous record of a date
I need to make a query, where I need to get the penultimate record, IE, the previous record and the date also. SELECT DISTINCT prt_partner.razao_social, prt_partner.end_cidade,…
-
3
votes1
answer81
viewsWhy create a table in Mysql?
I’m learning to work with Mysql and I’m seeing some examples of how to work with more than one table, as an example below: Id Nome Faculdade_id 101 Amanda 11 102 Bianca 12 103 Carla 13 104 Daniela…
-
3
votes2
answers1649
viewsOptimize a data transfer from one table to another SQL SERVER
SQL SERVER Database I have two tables, one with 2 billion records and the other with 16 billion. I am copying the first to the second. Using the INSERT INTO table2 SELECT * FROM table1; Doesn’t work…
-
3
votes1
answer241
viewsC# class as parameter in SQL Server
You can create a class in C# and the same class in SQL Server that represents a tabela (ID, Nome, Telefone) and create a precedent where I would pass as parameter this class . This is the class…
-
3
votes1
answer345
viewsSelect cities with the same name in different states
I have the table (result of a query) with the cities and their respective state... ... now only display cities with equal names and different states: How do I command?…
-
3
votes1
answer8878
viewsGenerate table Insert script in sql server
Using Sql Server 2012. I made a query with the title above and did not bring anything in the search. I have a BD and need to take it elsewhere. Therefore, the best way is to generate a script of all…
-
3
votes3
answers363
viewsProblem with Between in Mysql?
In an input and output control system there is a table cad_entrada_saida where it has two columns in the bank dEntrada and dSaida, these two columns are like varchar and recording the data in the…
-
3
votes1
answer531
viewsSQL select with dynamic Where in ABAP
There is the possibility to create a select with dynamic Where in the abap language?
-
3
votes1
answer5770
viewsWhat is the logic behind the trunc function?
What does the trunc to be sure and what other uses may have taking this example?…
-
3
votes1
answer53
views(SQL) Each property has multiple images and a cover image
Suppose I have a Real Estate table where a property can have multiple images. I have therefore another table, Images, with the url. I also have an intermediate table that makes the relationship N:N…
-
3
votes1
answer7859
viewsSql Server - View the View structure
I executed the following commands in the Sql Server database: comando 1: SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'vendas' resultado 1: TABLE_CATALOG TABLE_SCHEMA TABLE_NAME…
-
3
votes2
answers1940
viewsAdd a number in front of all records
A question arose when mounting an sql statement. I have several records in a table called db_contrato, I wanted to add the number 0 in front of all records, for now I defined the contract column as…
-
3
votes3
answers1235
viewsReplace value null
I wish that when you came null in my select replaced by 1900, follows select: SELECT (SELECT TOP 1 tabela1.data FROM tabela1 tabela1 WHERE tabela1.id = tabela2.id) as data FROM tabela2 tabela2…
-
3
votes1
answer278
viewsDecode vs Case Query
We have two ways to work with condition within consultations in Oracle SQL, are they the Case and the Decode, the two have the same function. Allow dynamically and practice how to get a return from…
-
3
votes3
answers5999
viewsseveral values for a column in Where
Is there any way to put several values for a column in WHERE? For example product A was replaced by product B, product 2933 for the 4044, product 2599 for 7845, and product 5987 by 9432. Example:…
-
3
votes1
answer96
viewsBring results from two tables (INNER JOIN error)
I have the table secretariats with the following structure: And the worker table with the following structure: I related the worker table to the secretariats table through the following list: That…
-
3
votes1
answer152
viewsHow to allocate transactions or group by day?
SELECT COUNT(*) QTD_TRANSACOES, CD_RSP_RECUSA, SUM(VL_AUTORIZADO) VALOR_SOMATORIO, CASE WHEN MR.DS_MOTIVO_RECUSA IS NULL THEN 'CÓDIGO DE RECUSA NÃO CADASTRADO' WHEN MR.DS_MOTIVO_RECUSA = 'APROVADO'…
-
3
votes3
answers161
viewsHow to perform a select by multiple fk?
I have a question about one (or more) select. I have the following bank created and I need to do a search for a certain recipe based on one (or more, and here’s the problem) ingredients. How can I…
-
3
votes1
answer95
viewsSELECT Union ALL Disregard Arguments
I have This query that sums two results in different tables. SELECT sum(g) saldo_anterior from ( SELECT SUM(valor_pg) g FROM ctrl_deposito WHERE MONTH(data_pg) < 11 and YEAR(data_pg) <= 2016…
-
3
votes2
answers57
viewsSuspend Trigger on parole
I did not find a SQL statenment IF in Sqlite and the closest was the CASE, that this attending me, meanwhile would like to suspend a Trigger upon a conditional and believe that can only be done with…
-
3
votes2
answers99
viewsGroup by mysql adding null values
How can I add empty values in group by? Example: I have an appointment that is organized by ages: CASE WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) < 4 THEN 'Menos…
-
3
votes1
answer4972
viewsSELECT in VIEW generates sub-query?
I wonder if I make one VIEW with a simple query, whether the view call generates a new SELECT, ie a sub-SELECT totaling 2 SELECTS or whether it only points to SELECT from within the VIEW? I only ran…
-
3
votes1
answer140
viewsQuery - Bring 5 major records
I have the query below, where I need to bring only the 2 largest columns that have the highest values. Today is coming out like this: Conciliacao Extrato Integração 150 140 80 That is, I wanted to…
-
3
votes2
answers72
viewsWhy is OR not good in a LEFT JOIN?
I’m creating a query and my pull request returned because the Code Star considered that the OR is not good for relationship tables. Example: FROM LEFT JOIN administracao.cidade cid ON…
-
3
votes2
answers878
viewsHow to get the number of columns of a temporary table
How is it possible to obtain the number of existing columns in a given temporary table in sql server?
-
3
votes3
answers1439
viewsDifference between SQL SERVER dates
I have a question on sql server. Based on the table below, how can I catch the difference in days which takes to go from one phase to the other of the same client, ie difference phase to phase.…
-
3
votes4
answers1722
viewsHow to recover a value from within a tag in XML?
I have a table called SPDNFE with a column DsXML which contains all the contents of a Xml. Query: SELECT top 1 SPDNFE.DsXML FROM SPDNFE where SPDNFE.CdIdNFe =…
-
3
votes2
answers488
viewsBest way to relate two tables
I have two tables (groups and roles): Table groups: +----+------+-------+------+ | id | name | color | role | +----+------+-------+------+ Table roles: +----+------+-------+-------------+ | id |…
-
3
votes3
answers559
viewsDocument protocol
I am making a program in php, mysql and would like to generate a protocol with some argument. The protocol would be generated automatically in the system, without interference from the user, but it…
-
3
votes2
answers1365
viewsHow to make paging in Sqlserver 2008 R2?
I tested with two forms, but did not succeed. select * from table OFFSET 10 ROWS select top 10 * from table OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY Interestingly I never had to offset this version of…
-
3
votes1
answer479
viewsSelect Like in Oracle
I’m developing a routine to standardize the descriptions in an Oracle database. So I’m scanning the entire database where I select the description, extract it word for word and compare it to a…
-
3
votes1
answer223
viewsHow to join two selects of different tables?
I have the following tables: +--------- + +-------------+ | usuario | |usuario_grupo| +----------+ +--------- + +-------------+ | grupo | |usuario_id|----->| membro_id | +----------+ | nome | |…
-
3
votes1
answer64
viewsHelp! How this select works
Help me understand how this works please. I happen to be starting the studies in SQL and hackerhank has an exercise that needed this code: SELECT DISTINCT CITY FROM STATION WHERE…
-
3
votes2
answers254
viewsGroup By different results
Hi, I have a question if anyone can help me on SQL. I know the Group By clause, as far as I know it groups similar data. But I understand that it should group but in the script below it has the…
-
3
votes2
answers932
viewsGROUP BY SQL error
I have the following exercise: List the department code and name, newest and oldest admission date of each department, sort by department name. Here’s what I did: select d.department_id,…
-
3
votes1
answer50
viewsSQL Problems with query
I am having problems with the following query: SELECT Cliente.Nome, (Reserva.NumeroNoites) TotalNoites From Reserva inner join Cliente on Reserva.ID_Cliente = Cliente.ID_Cliente group by…
-
3
votes3
answers1110
viewsHow to find "holes" in SQL Server tables?
I have a table with a column id (primary key, auto increment value 1). My application nay allows lines to be excluded, hence the expected query SELECT id FROM tbl ORDER BY id ASC that would be it:…
-
3
votes2
answers1482
viewsSQL: Doubt in the Relationship between Tables
I am creating an academic system that consists of a virtual restaurant menu type, for this, I created three tables in the bank: Table of Menu Items: CREATE TABLE ITEMS( ID INT PRIMARY KEY NOT NULL,…
-
3
votes3
answers203
viewsHow to show a column, but if null show another sql
How to show a column, but if null show another sql as Example a table with the name tasks. | id | nome | nome resumido | +-----+----------+----------------+ | 1 | nome 1 | 1 | | 2 | nome 2 | 2 | | 3…